且构网

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

当从扩展类调用时,groovy中的Closure不能使用私有字段

更新时间:2023-12-05 16:53:52

Groovy中的默认访问修饰符是 public ,当Groovy在类生成时自动添加访问器方法时,可以无缝创建POGO。

Default access modifier in Groovy is public, which helps in creating POGOs seamlessly as Groovy adds the accessor methods automatically at class generation.

当访问修饰符更改为 private ,groovy不会为该属性创建任何访问器方法。为了访问作为只读属性的私有属性,
getJSessionIdCookie()方法必须添加到基类中。

When the access modifier is changed to private, groovy does not create any accessor method for that property. In order to access that private property as a read-only property getJSessionIdCookie() method has to be added to the base class.

getJSessionIdCookie() { jSessionIdCookie }

现在,当您在子类中访问 jSessionIdCookie 时, getProperty 元类实现将调用上述getter方法。

Now, when you access jSessionIdCookie in sub-class, getProperty metaclass implementation will invoke the above getter method instead.