java @sign 有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11151633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:59:13  来源:igfitidea点击:

What does the @sign do?

javagroovy

提问by Parham Doustdar

I have seen the at (@) sign in Groovy files and I don't know if it's a Groovy or Java thing. I have tried to search on Google, Bing, and DuckDuckGo for the mystery at sign, but I haven't found anything. Can anyone please give me a resource to know more about what this operator does?

我在 Groovy 文件中看到了 at (@) 符号,我不知道它是 Groovy 还是 Java 的东西。我曾尝试在 Google、Bing 和 DuckDuckGo 上搜索标志的神秘面纱,但我什么也没找到。任何人都可以给我一个资源来了解更多关于这个运营商做什么的信息吗?

采纳答案by Josh

It's a Java annotation. Read more at that link.

这是一个 Java注释。在该链接上阅读更多内容。

回答by tim_yates

As well as being a sign for an annotation, it's the Groovy Field operator

除了作为注释的标志外,它还是Groovy Field 运算符

In Groovy, calling object.fieldcalls the getFieldmethod (if one exists). If you actually want a direct reference to the field itself, you use @, ie:

在 Groovy 中,调用会object.field调用getField方法(如果存在)。如果您确实想要直接引用字段本身,请使用@,即:

class Test {
  String name = 'tim'

  String getName() {
    "Name: $name"
  }
}

def t = new Test()
println t.name   // prints "Name: tim"
println t.@name  // prints "tim"

回答by Rhysyngsun

It can also be used to access attributes when parsing XML using Groovy's XmlSlurper:

在使用 Groovy 的 XmlSlurper 解析 XML 时,它还可以用于访问属性:

def xml = '''<results><result index="1"/></results>'''
def results = new XmlSlurper().parseText(xml)
def index = results.result[0][email protected]() // prints "1"

http://groovy.codehaus.org/Reading+XML+using+Groovy's+XmlSlurper

http://groovy.codehaus.org/Reading+XML+using+Groovy's+XmlSlurper

回答by Hemant Metalia

'@' is an annotations in java/ Groovylook at the demo :Example with code

'@' 是 java/Groovy 中的一个注解看演示 : Example with code

Java 5 and above supports the use of annotations to include metadata within programs.Groovy 1.1 and above also supports such annotations.

Java 5 及更高版本支持使用注释在程序中包含元数据。Groovy 1.1 及更高版本也支持此类注释。

  • Annotations are used to provide information to tools and libraries.

  • They allow a declarative style of providing metadata information and allow it to be stored directly in the source code.

  • Such information would need to otherwise be provided using non-declarative means or using external files.
  • 注释用于为工具和库提供信息。

  • 它们允许提供元数据信息的声明式风格,并允许它直接存储在源代码中。

  • 否则需要使用非声明方式或使用外部文件提供此类信息。