Groovy @字段前的符号 [英] Groovy @ symbol before fields

查看:231
本文介绍了Groovy @字段前的符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<@> @ Groovy中的字段名称之前的@是什么意思?对于某些班级,我可以访问不能直接访问的私人字段,我们来看看 ComposedClosure 例如:

  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 //第一个闭包对象
println a.first //运行时错误


解决方案

它允许您覆盖groovy对属性访问器的使用。如果你写:

  println u.name 

groovy会调用自动生成的getter Person.getName()。如果你写:

  println u。@ name 

它将直接进入该领域,就像在Java中一样。在关闭的情况下,它似乎有一个第一个字段,但不是相应的 getFirst 访问器。



在groovy手册中,它被记录为直接字段访问运营商

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

解决方案

It allows you to override groovy's use of property accessors. If you write:

println u.name

groovy will invoke the automatically generated getter Person.getName(). If you write:

println u.@name

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 corresponding getFirst accessor.

In the groovy manual, it's documented as the direct field access operator.

这篇关于Groovy @字段前的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆