我刚刚发现了这个功能.
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.
用法:你有一个注解@HelloWorld
和一个属性sayHello
.你可以把它放在这样的类上:
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 默认声明用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!