字段前的 Groovy @ 符号
Groovy 中字段名称前面的 @ 意味着什么?对于某些类,我能够访问无法直接访问的私有字段,让我们采取 CompositionClosure 例如:
public class Person {
private String name
}
def u = new Person(name:"Ron")
println u.@name //Ron
println u.name //Ron
a = {2} >> {3}
println a.@first //first closure object
println a.first //runtime error
What does @ means before a field name in Groovy? For some classes I am able to access private fields that are not directly accessible, let's take ComposedClosure for example:
public class Person {
private String name
}
def u = new Person(name:"Ron")
println u.@name //Ron
println u.name //Ron
a = {2} >> {3}
println a.@first //first closure object
println a.first //runtime error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它允许您覆盖 groovy 对属性访问器的使用。如果你这样写:
groovy 将调用自动生成的 getter Person.getName()。如果你写:
它将像在Java中一样直接进入该字段。在闭包的情况下,它似乎有一个
first
字段,但没有相应的getFirst
访问器。在 groovy 手册中,它被记录为直接字段访问运算符。
It allows you to override groovy's use of property accessors. If you write:
groovy will invoke the automatically generated getter Person.getName(). If you write:
it will go directly to the field like it would in Java. In the case of the closure, it seems to have a
first
field but not a correspondinggetFirst
accessor.In the groovy manual, it's documented as the direct field access operator.
这意味着您直接访问字段,而不是通过 getter。
请参阅 Groovy 运算符文档,尽管没有更多可说的。除了可能避免它之外。
ComposeClosure
失败的原因是first
(或second
)没有 getter。It means you're accessing a field directly, rather than going through a getter.
See the Groovy operator docs, although there isn't much more to say. Other than probably avoid it.
The reason it fails for a
ComposedClosure
is because there's no getter forfirst
(orsecond
).