开发者

How do I disable generation of Groovy accessors?

Groovy 开发者_Python百科Beans are great but I'm just curious if it's possible to declare a class member private and not generate accessors for it easily? The http://groovy.codehaus.org/Groovy+Beans>Groovy Beans page doesn't cover this topic. The only thing that I can think of would be to define the accessors and make them private.


Groovy won't add accessors if the member is declared with an access modifier: private, protected or public. If you don't want accessors, just add whichever modifier is appropriate. Here's an example that illustrates this:

class Test1 { private int blat }
println Test1.metaClass.getMethods()*.name.findAll { it.endsWith("Blat") }
class Test2 { protected int blat }
println Test2.metaClass.getMethods()*.name.findAll { it.endsWith("Blat") }
class Test3 { public int blat }
println Test3.metaClass.getMethods()*.name.findAll { it.endsWith("Blat") }
class Test4 { int blat }
println Test4.metaClass.getMethods()*.name.findAll { it.endsWith("Blat") }

Prints:

[]
[]
[]
[getBlat, setBlat]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜