問題描述
我剛剛發現了這個功能.
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模板網!