且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Groovy-get和propertyMissing之间的区别?

更新时间:2023-12-04 11:27:16

嗯,不,它们是不一样的,如下所示:

Well, no, they're not the same, as is evidenced by the following:

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.

The normal get (and getProperty) method checks to see if the property exists, then calls propertyMissing if it doesn't.

重载get时,会丢失propertyMissing功能.

When you overloaded get, you lost the propertyMissing functionality.