-
-
Save fumokmm/890729 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hello { | |
void hello(){ println "1" } | |
} | |
def a = new Hello() | |
def b = new Hello() | |
println '-' * 10 | |
b.metaClass.hello = { println "2" } | |
print '[1] '; a.hello() // => 1 | |
print '[2] '; b.hello() // => 2 (metaClass.hello が呼ばれる) | |
println '-' * 10 | |
a.metaClass.hello = { b.metaClass.hello } | |
print '[3] '; a.hello() // => 結果はb.metaClass.hello(groovy.lang.ExpandoMetaClass$ExpandoMetaProperty) | |
print '[4] '; b.hello() // => bのhelloはLine8の定義が生きているので (print "2")が呼び出される | |
println '-' * 10 | |
a.metaClass.hello = { println "3" } | |
print '[5] '; a.hello() // => Line18の定義になる (print "3") | |
print '[6] '; b.hello() // => bのhelloはLine8の定義が生きているので (print "2") が呼び出される | |
print '[7] '; a.&hello() // => Line2で定義した void hello()のメソッドクロージャを取得しつつ実行しているため (print "1") | |
println '-' * 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
---------- | |
[1] 1 | |
[2] 2 | |
---------- | |
[3] [4] 2 | |
---------- | |
[5] 3 | |
[6] 2 | |
[7] 1 | |
---------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment