在Java中@interface默认使用的声明 [英] @interface default declaration usage in Java

查看:630
本文介绍了在Java中@interface默认使用的声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现了这个功能。

使用@interface语法允许你把一个缺省值声明的接口。

 公共@interface的HelloWorld {
     公共字符串的sayHello()默认的Hello World;
}

这对我来说是新的东西。如何是假设使用的默认值。

我找不到引用,因为www是完整的Java接口文件之前,@除了用Java 1.5(是它0.5或在0.4?)


修改

感谢您的答案<子>(我在某种程度上接近注释,因为我使用的标签的话):P

我知道我应该读过文档年前!!! ...让我们来看看...


  

很多的API需要样板code相当数量。对于....



解决方案

您刚才写的的标注

特别是关于在默认声明:这是使用,因为注释和接口不能有构造函数,所以这是有注释的默认值的唯一途径属性。从 Java语言规范


  

这是注释类型元件可以具有用于它指定的默认​​值。这是通过关键字默认和元素的默认值以下的(空的)参数列表完成。


  
  

默认值是在注释读取时间动态应用;默认值不会被编译到注释。因此,改变默认值会影响甚至进行了更改(presuming这些注释缺乏对违约元素的显式值)之前已编译的类注释。


我注意到,没有注释的<一个href=\"http://java.sun.com/javase/6/docs/api/java/lang/annotation/package-summary.html\">java.lang.annotation使用默认值,虽然。


用法:您有一个注释 @HelloWorld 与属性的sayHello 。你可以把它放在一类是这样的:

  @HelloWorld(sayHello的=HI)
公共类MyClass的{
}

既然你有一个默认值,你可以只是把

  @HelloWorld
公共类MyClass的{
}

(注意,该文件说:在注释只有一个元素,该元素应该被命名为;我相信这样做的唯一原因就是你可以只写 @HelloWorld(你好)无需名称的参数。)

由于写的,你的注解可以是任何合法程序元素(包括方法和变量声明)上使用。您可以更改此<一个href=\"http://java.sun.com/javase/6/docs/api/java/lang/annotation/Target.html\"><$c$c>@Target注释。

最后,设置<一个href=\"http://java.sun.com/javase/6/docs/api/java/lang/annotation/RetentionPolicy.html\"><$c$c>RetentionPolicy让你决定是否注释应该由编译器被丢弃,由VM抛弃,始终保持。


两个包,可能也很有趣:<一href=\"http://java.sun.com/javase/6/docs/api/javax/annotation/package-summary.html\">javax.annotation和<一个href=\"http://java.sun.com/javase/6/docs/api/javax/annotation/processing/package-summary.html\">javax.annotation.processing.和<一个href=\"http://today.java.net/pub/a/today/2008/04/10/source-$c$c-analysis-using-java-6-compiler-apis.html\">here是使用注释处理源$ C ​​$ C分析的一个例子。

I have just discovered this feature.

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.

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? )


EDIT

Thanks for the answers ( I was somehow close to "annotation", for I use the tag already ) :P

I knew I should've read that document years ago!!!... let's see...

Many APIs require a fair amount of boilerplate code. For....

解决方案

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:

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).

I note that none of the annotations in java.lang.annotation use default values, though.


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 {
}

(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.)

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.

Finally, setting the RetentionPolicy lets you decide if the annotation should be discarded by the compiler, discarded by the VM, or kept always.


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆