Java中的@interface默认声明用法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/588056/
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
@interface default declaration usage in Java
提问by OscarRyz
I have just discovered this feature.
我刚刚发现了这个功能。
Declaring an interface using the "@interface" syntax allows you to put a default value.
使用“@interface”语法声明接口允许您放置默认值。
public @interface HelloWorld {
public String sayHello() default "hello world";
}
This is something new for me. How is that default value suppose to be used.
这对我来说是新事物。假设如何使用该默认值。
I cannot find references to that, because the www is full of java interface documents prior to "@" addition in Java 1.5 ( was it on .5 or in .4? )
我找不到对它的引用,因为在 Java 1.5 中添加“@”之前,www 充满了 Java 接口文档(它是在 .5 还是在 .4 中?)
EDIT
编辑
Thanks for the answers ( I was somehow close to "annotation", for I use the tag already ) :P
感谢您的回答(我以某种方式接近“注释”,因为我已经使用了标签):P
I knew I should've read that document years ago!!!... let's see...
我知道我应该在几年前读过那份文件!!!...让我们看看...
Many APIs require a fair amount of boilerplate code. For....
许多 API 需要大量样板代码。为了....
采纳答案by Michael Myers
You have just written an annotation.
你刚刚写了一个注释。
Regarding the default
statement in particular: This is used because annotations and interfaces can't have constructors, so this is the only way to have a default value for an annotation attribute. From the Java Language Specification:
default
特别是关于声明:这是因为注释和接口不能有构造函数,所以这是为注释属性设置默认值的唯一方法。来自Java 语言规范:
An annotation type element may have a default value specified for it. This is done by following its (empty) parameter list with the keyword
default
and the default value of the element.Defaults are applied dynamically at the time annotations are read; default values are not compiled into annotations. Thus, changing a default value affects annotations even in classes that were compiled before the change was made (presuming these annotations lack an explicit value for the defaulted element).
注释类型元素可能具有为其指定的默认值。这是通过在其(空)参数列表后面加上关键字
default
和元素的默认值来完成的。在读取注释时动态应用默认值;默认值不会编译为注释。因此,即使在进行更改之前编译的类中,更改默认值也会影响注释(假设这些注释缺少默认元素的显式值)。
I note that none of the annotations in java.lang.annotationuse default values, though.
我注意到java.lang.annotation中的所有注释都没有使用默认值。
Usage:You have an annotation @HelloWorld
with an attribute sayHello
. You could put it on a class like this:
用法:您有一个@HelloWorld
带有属性的注释sayHello
。你可以把它放在这样的类上:
@HelloWorld(sayHello="Hi")
public class MyClass {
}
Since you have a default value, you could just put
因为你有一个默认值,你可以把
@HelloWorld
public class MyClass {
}
(Note that the document says, "In annotations with a single element, the element should be named value
"; I believe the only reason to do this is that you could just write @HelloWorld("Hi")
without having to name the parameter.)
(请注意,文档说,“在具有单个元素的注释中,元素应该被命名为value
”;我相信这样做的唯一原因是你可以直接编写@HelloWorld("Hi")
而不必命名参数。)
As written, your annotation can be used on any valid program element (including methods and variable declarations). You can change this with the @Target
annotation.
正如所写的那样,您的注释可以用于任何有效的程序元素(包括方法和变量声明)。您可以使用@Target
注释更改此设置。
Finally, setting the RetentionPolicy
lets you decide if the annotation should be discarded by the compiler, discarded by the VM, or kept always.
最后,设置RetentionPolicy
可以让您决定注释是应该被编译器丢弃、被 VM 丢弃还是始终保留。
Two packages that might also be interesting: javax.annotationand javax.annotation.processing. And hereis an example of using annotation processing for source code analysis.
两个可能也很有趣的包:javax.annotation和javax.annotation.processing。和这里是使用注释处理源代码分析的一个例子。
回答by ScArcher2
That's an annotationyou are declaring not an interface. It was added in Java 1.5.
这是您声明的注释而不是接口。它是在 Java 1.5 中添加的。