Groovy-get和propertyMissing之间的区别?
我缺少了一些东西。据我所见,如果没有可用的属性、访问器或getProperty,get是最后的手段。实际上,这不意味着get和propertyMissing做相同的事情吗?我知道get扩展了field access操作符,因此那里一定发生了一些事情:-)Groovy-get和propertyMissing之间的区别?,groovy,Groovy,我缺少了一些东西。据我所见,如果没有可用的属性、访问器或getProperty,get是最后的手段。实际上,这不意味着get和propertyMissing做相同的事情吗?我知道get扩展了field access操作符,因此那里一定发生了一些事情:-) 嗯,不,它们不一样,如下所示: class Baz { String name = 'bob' Object propertyMissing(String name) { 'not called' }
嗯,不,它们不一样,如下所示:
class Baz {
String name = 'bob'
Object propertyMissing(String name) {
'not called'
}
}
Baz b = new Baz()
assert b.getProperty('name') == 'bob'
assert b.getProperty('whatever') == 'not called'
assert b.name == 'bob'
assert b.whatever == 'not called'
正常的get(和getProperty)方法检查属性是否存在,如果不存在,则调用propertyMissing
当您过载get时,您将失去propertyMissing功能。谢谢您的解释
class Baz {
String name = 'bob'
Object propertyMissing(String name) {
'not called'
}
}
Baz b = new Baz()
assert b.getProperty('name') == 'bob'
assert b.getProperty('whatever') == 'not called'
assert b.name == 'bob'
assert b.whatever == 'not called'