Java中的@interface默认声明用法 [英] @interface default declaration usage in Java
问题描述
我刚刚发现了这个功能.
I have just discovered this feature.
使用@interface"语法声明接口允许您放置默认值.
Declaring an interface using the "@interface" syntax allows you to put a default value.
public @interface HelloWorld {
public String sayHello() default "hello world";
}
这对我来说是新事物.假设如何使用该默认值.
This is something new for me. How is that default value suppose to be used.
我找不到对它的引用,因为在 Java 1.5 中添加@"之前,www 充满了 Java 接口文档(它是在 .5 还是在 .4 中?)
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? )
编辑
感谢您的回答(我有点接近注释",因为我已经使用了标签):P
我知道我应该在几年前读过那份文件!!!...让我们看看...
I knew I should've read that document years ago!!!... let's see...
许多 API 需要大量样板代码.对于....
推荐答案
您刚刚编写了一个 注释.
特别是关于 default
语句:使用它是因为注释和接口不能有构造函数,所以这是为注释属性设置默认值的唯一方法.来自 Java 语言规范:
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
和元素的默认值来完成的.
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).
我注意到 java.lang.annotation 使用默认值.
I note that none of the annotations in java.lang.annotation use default values, though.
用法:您有一个带有属性 sayHello
的注释 @HelloWorld
.你可以把它放在这样的类上:
Usage: You have an annotation @HelloWorld
with an attribute sayHello
. You could put it on a class like this:
@HelloWorld(sayHello="Hi")
public class MyClass {
}
既然你有一个默认值,你就可以把
Since you have a default value, you could just put
@HelloWorld
public class MyClass {
}
(请注意,文档说,在具有单个元素的注释中,该元素应命名为 value
";我相信这样做的唯一原因是您可以只编写 @HelloWorld("Hi")
无需命名参数.)
(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.)
如所写,您的注解可用于任何有效的程序元素(包括方法和变量声明).您可以使用 @Target 更改此设置
注释.
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.
最后,设置RetentionPolicy
让您决定是编译器丢弃注释、VM 丢弃注释还是始终保留注释.
Finally, setting the RetentionPolicy
lets you decide if the annotation should be discarded by the compiler, discarded by the VM, or kept always.
两个可能也很有趣的包:javax.annotation 和 javax.annotation.processing.和 这里是一个使用注解处理进行源码分析的例子.
Two packages that might also be interesting: javax.annotation and javax.annotation.processing. And here is an example of using annotation processing for source code analysis.
这篇关于Java中的@interface默认声明用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!