“æ£åœ¨å› ä½ çš„è°ƒç”¨è¯å¥è€Œæ‰§è¡Œçš„æ–¹æ³•ï¼Œå°±çœŸçš„æ˜¯ä½ æœŸæœ›ç€è¢«æ‰§è¡Œçš„那一个实现å—?†å¶ç„¶çš„一次merge为我的ruby代ç 带æ¥ä¸€ä¸ªæœ‰è¶£çš„问题。
相åŒç¾å的方法
1)Rubyç±»ä¸èµ¤è£¸è£¸åœ°å‡ºçŽ°ä¸¤ä¸ªç¾å一致的方法,被调用时ä¸ä¼šæŠ¥å‘Šé”™è¯¯çš„。åŒå±‚级的多个相åŒç¾å的方法,被认å¯ã€è¢«è°ƒç”¨çš„æ˜¯æœ€åŽä¸€å¤„定义。
class TestMethodCalling def say_hello puts "hello, I'am the implementation 1." end def say_hello puts "hello, I'am the implementation 2." end end instance = TestMethodCalling.new instance.say_hello
ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼
> hello, I'am the implementation 2.
2)Rubyåç±»ä¸å¯ç›´æŽ¥é‡å¤å®šä¹‰çˆ¶ç±»ä¸çš„已有方法,å类方法被调用。
class SuperClass def say_hello puts "hello, I'am the implementation 4." end end class TestMethodCalling < SuperClass def say_hello puts "hello, I'am the implementation 1." end end instance = TestMethodCalling.new instance.say_hello
ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼
> hello, I'am the implementation 1.
3)通过Mix’in的方å¼å°†Module的方法引入类ä¸ï¼Œç±»ä¸æ–¹æ³•被优先调用。
module SayingModule def say_hello puts "hello, I'am the implementation 3." end end class TestMethodCalling include SayingModule def say_hello puts "hello, I'am the implementation 1." end end instance = TestMethodCalling.new instance.say_hello
ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼
> hello, I'am the implementation 1.
4)通过Mix‘in的方å¼å°†Module的方法引入åç±»ä¸ï¼Œåç±»ä¸å¹¶æ— æ–¹æ³•å®šä¹‰ï¼Œä½†çˆ¶ç±»ä¸æœ‰ï¼ŒModule方法被调用。
module SayingModule def say_hello puts "hello, I'am the implementation 3." end end class SuperClass def say_hello puts "hello, I'am the implementation 4." end end class TestMethodCalling < SuperClass include SayingModule end instance = TestMethodCalling.new instance.say_hello
ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼
> hello, I'am the implementation 3.
5)被调用方法未定义时,Rubyä»èƒ½åŒ…容,它会寻找method_missing(method_name)方法并调用,其ä¸method_name是被调用方法å称;若找ä¸åˆ°ï¼Œé‚£ä¹ˆæ‰æŠ¥å‡ºundefined method错误。
class TestMethodCalling def method_missing(m) puts "missing method name is '#{m}'." end end instance = TestMethodCalling.new instance.not_existing_method
ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼
> missing method name is 'not_existing_method'.
å°ç»“
Ruby解释执行方法的调用时,会按照current class ï¼>Module ï¼>parent class的优先顺åºå¯»æ‰¾æ–¹æ³•定义;当在åŒä¸€å±‚级下é‡åˆ°å¤šå¤„相åŒç¾å的方法定义时,执行最åŽä¸€ä¸ªã€‚
é‡å¤åç§°çš„class/module
Ruby䏿¢å¯¹æ–¹æ³•定义和调用展现其包容性,对待类和module也能如æ¤ã€‚
1)对类和module而言,分散在多处的åŒå称定义å¯çœ‹ä½œè¯¥ç±»æˆ–moduleçš„å„个组æˆéƒ¨åˆ†ï¼Œå®žä¾‹å˜é‡çš„作用域贯穿å„处的方法定义。
2)有多处åŒå的类定义或module定义时,ç¾å相åŒçš„æ–¹æ³•è¢«æ‰§è¡Œçš„ä»æ˜¯æœ€åŽä¸€å¤„。当这些定义ä¸åœ¨åŒä¸€ä¸ªæ–‡ä»¶ä¸æ—¶ï¼Œä¼šæŒ‰ç…§require文件的顺åºä¸çš„æœ€åŽä¸€å¤„选择。
module SayingModule def say_yes puts "Yes, sir. I'am in the module. #{@me}" end end class SuperClass def say_hello puts "hello, I'am in super class." end end class TestMethodCalling < SuperClass def say_hello puts "hello, I'am the implementation 1." end def say_bye puts "Bye, I'am the implementation 1. #{@me}" end end class TestMethodCalling < SuperClass def say_hello @me = "shaobo" puts "hello, I'am the implementation 2.1. #{@me}" end include SayingModule end instance = TestMethodCalling.new instance.say_hello instance.say_yes instance.say_bye
ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼ï¼
> hello, I'am the implementation 2.1. shaobo
> Yes, sir. I'am in the module. shaobo
> Bye, I'am the implementation 1. shaobo
ä¸å¾—䏿‰¿è®¤Ruby太开放了,动æ€è§£é‡Šçš„强大包容力是在编译型è¯è¨€é‡Œæƒ³éƒ½æƒ³ä¸åˆ°çš„。
Java说,相åŒç¾å的方法ä¸èƒ½åŒæ—¶å˜åœ¨åœ¨ä¸€ä¸ªç±»é‡Œï¼›
C#说,å类里å¤å†™çˆ¶ç±»çš„æ–¹æ³•è¦åР关键å—ï¼›
C++说,继承了多个父类åŽï¼Œä½¿ç”¨å£°æ˜Žç›¸åŒçš„父类æˆå‘˜æ—¶è¦æŒ‡æ˜Žç”¨å“ªä¸ªçˆ¶ç±»çš„ï¼›
⋯⋯
Rubyè¯´ï¼Œä½ ä»¬é‚£äº›éƒ½æ˜¯æµ®äº‘ï¼Œåœ¨æˆ‘è¿™å„¿æ²¡é‚£ä¹ˆå¤šè§„çŸ©ã€‚