1
有些東西我缺少。從我所看到的,得到是最後的手段,如果沒有財產,accessor或getProperty可用。實際上,這是不是意味着得到和propertyMissing做同樣的事情?我知道得到擴展了現場訪問運算符所以必須有一些事情有:-)Groovy - get和propertyMissing之間的區別?
// Using get
class Foo {
def name = 'Jahg'
Object get(String name) {
'called get'
}
}
def f1 = new Foo()
assert f1.noexist == 'called get'
// get() is not called for the known property (name)
assert f1.name == 'Jahg'
// Using propertyMissing
class Bar {
Object propertyMissing(String name) {
'called propertyMissing'
}
}
def f2 = new Bar()
assert f2.noexist == 'called propertyMissing'
// When both are defined, get() takes precedence
class Baz {
// This one is called
Object get(String name) {
'called get'
}
Object propertyMissing(String name) {
'not called'
}
}
def f3 = new Baz()
assert f3.noexist == 'called get'
謝謝你解釋它 –