请输入您要查询的百科知识:

 

词条 Annotation
释义

java.lang.annotation,接口 Annotation。对于Annotation,是Java5的新特性,JDK5引入了Metedata(元数据)很容易的就能够调用Annotations.Annotations提供一些本来不属于程序的数据,比如:一段代码的作者或者告诉编译器禁止一些特殊的错误。An annotation 对代码的执行没有什么影响。Annotations使用@annotation的形势应用于代码:类(class),属性(field),方法(method)等等。一个Annotation出现在上面提到的开始位置,而且一般只有一行,也可以包含有任意的参数。

所有已知实现类

Deprecated, Documented, Inherited, Override, Retention, SuppressWarnings, Target

--------------------------------------------------------------------------------

public interface Annotation所有 annotation 类型都要扩展的公共接口。注意,手动扩展该公共接口的接口不 定义 annotation 类型。还要注意此接口本身不定义 annotation 类型。

对于Annotation,是Java5的新特性,下面是Sun的Tutorial的描述,因为是英文,这里我翻译下,希望能够比较清晰的描述一下Annotation的语法以及思想。Annotation:Release 5.0 of the JDK introduced a metadata facility called annotations. Annotations provide data about a program that is not part of the program, such as naming the author of a piece of code or instructing the compiler to suppress specific errors. An annotation has no effect on how the code performs. Annotations use the form @annotation and may be applied to a program's declarations: its classes, fields, methods, and so on. The annotation appears first and often (by convention) on its own line, and may include optional arguments: JDK5引入了Metadata(元数据)很容易的就能够调用Annotations.Annotations提供一些本来不属于程序的数据,比如:一段代码的作者或者告诉编译器禁止一些特殊的错误。An annotation 对代码的执行没有什么影响。Annotations使用@annotation的形式应用于代码:类(class),属性(field),方法(method)等等。一个Annotation出现在上面提到的开始位置,而且一般只有一行,也可以包含有任意的参数。@Author("MyName")class myClass() { }

or @SuppressWarnings("unchecked")void MyMethod() { }

Defining your own annotation is an advanced technique that won't be described here, but there are three built-in annotations that every Java programmer should know: @Deprecated, @Override, and @SuppressWarnings. The following example illustrates all three annotation types, applied to methods:

定义自己的Annotation是一个比较高级的技巧,这里我们不做讨论,这里我们仅仅讨论每一个Java programer都应该知道的内置的annotations:@Deprecated, @Override, and @SuppressWarnings。下面的程序阐述了这三种annotation如何应用于methods。import java.util.List;

class Food {}

class Hay extends Food {}

class Animal {

Food getPreferredFood() {

return null;

} /** * @deprecated document why the method was deprecated */

@Deprecated

static void deprecatedMethod() { }

}

class Horse extends Animal {

Horse() {

return;

}

@Override

Hay getPreferredFood() {

return new Hay();

}

@SuppressWarnings("deprecation")

void useDeprecatedMethod() {

Animal.deprecateMethod(); //deprecation warning - suppressed }}

}

}

@Deprecated The @Deprecated annotation indicates that the marked method should no longer be used. The compiler generates a warning whenever a program uses a deprecated method, class, or variable. When an element is deprecated, it should be documented using the corresponding @deprecated tag, as shown in the preceding example. Notice that the tag starts with a lowercase "d" and the annotation starts with an uppercase "D". In general, you should avoid using deprecated methods — consult the documentation to see what to use instead.

@Deprecated @Deprecated annotation标注一个method不再被使用。编译器在一个program(程序?)使用了不赞成的方法,类,变量的时候会产生警告(warning)。如果一个元素(element:method, class, or variable)不赞成被使用,应该像前面的例子里使用相应的@deprecated 标签,并且注意标签的首字母是小写的"d",而annotation时大写的"D"。一般情况下,我们应该避免使用不赞成使用的方法(deprecated methods),而应该考虑替代的方法。

@Override The @Override annotation informs the compiler that the element is meant to override an element declared in a superclass. In the preceding example, the override annotation is used to indicate that the getPreferredFood method in the Horse class overrides the same method in the Animal class. If a method marked with @Override fails to override a method in one of its superclasses, the compiler generates an error. While it's not required to use this annotation when overriding a method, it can be useful to call the fact out explicitly, especially when the method returns a subtype of the return type of the overridden method. This practice, called covariant return types, is used in the previous example: Animal.getPreferredFood returns a Food instance. Horse.getPreferredFood (Horse is a subclass of Animal) returns an instance of Hay (a subclass of Food). For more information, see Overriding and Hiding Methods.

@Override @Override annotation 告诉编译器当前元素是重写(override)自父类的一个元素。在前面的例子中,override annotation用来说明Horse类中的getPreferredFood这个方法重写(override)自Animal类中相同的方法。如果一个方法被标注了@Override,但是其父类中没有这个方法时,编译器将会报错。但是并不是说我们一定要使用这个annotation,但是它能够很明显的给出实际行为,尤其是在方法返回一个被重写的方法返回类型的子类型的时候。上面的例子中,Animal.getPreferredFood 返回一个 Food实例,Horse.getPreferredFood 返回一个Hay实例,这里Horse是Animal的子类,Hay是Food的子类。

@SuppressWarnings The @SuppressWarnings annotation tells the compiler to suppress specific warnings that it would otherwise generate. In the previous example, the useDeprecatedMethod calls a deprecated method of Animal. Normally, the compiler generates a warning but, in this case, it is suppressed. Every compiler warning belongs to a category. The Java Language Specification lists two categories: "deprecation" and "unchecked". The "unchecked" warning can occur when interfacing with legacy code written before the advent of generics. To suppress more than one category of warnings, use the following syntax: @SuppressWarnings({"unchecked", "deprecation"})

@SuppressWarnings annotation 告诉编译器禁止别的元素产生的特殊的警告(warnings),在前面的例子里,useDeprecatedMethod调用了Animal的不赞成使用的一个方法。一般情况下,编译器会给出一个警告(warning),但是在这种情况下,不会产生这个警告,也就是说被suppress。每个编译器的警告都属于一个类型。Java Language Specification列出了两种类型:"deprecation" 和 "unchecked"。"unchecked" 警告发生在使用非generic的旧代码交互的generic collection类时。要禁止不止一种的警告时,则使用下面的语法:@SuppressWarnings({"unchecked", "deprecation"})

从以下版本开始

1.5

方法摘要

Class<? extends Annotation> annotationType()

返回此 annotation 的注释类型。

boolean equals(Object obj)

如果指定的对象表示在逻辑上等效于此接口的注释,则返回 true。

int hashCode()

返回此 annotation 的哈希代码,具体说明如下: 一个 annotation 的哈希代码是其成员(包括那些带有默认值的成员)的哈希代码的和,具体说明如下: annotation 成员的哈希代码是成员值哈希代码的 XOR(它是 String.hashCode() 计算得到的成员名哈希代码的 127 倍),具体说明如下: 成员值的哈希代码取决于其类型: 基值 v 的哈希代码等于 WrapperType.valueOf(v).hashCode(),其中 WrapperType 是对应 v 的基本类型的包装器类型(Byte、Character、Double、Float、Integer、Long、Short 或 Boolean)。

String toString()

返回此 annotation 的字符串表示形式。

方法详细信息

equals

boolean equals(Object obj)如果指定的对象表示在逻辑上等效于此接口的注释,则返回 true。换句话说,如果指定对象是一个与此实例相同的 annotation 类型的实例,即其所有成员都与此实例中所对应的成员相等,则返回 true,具体说明如下:

如果 x == y,则认为值分别为 x 和 y 的两个对应的基本类型成员相等,除非它们的类型是 float 或 double。

如果 Float.valueOf(x).equals(Float.valueOf(y)) 为真,则认为值分别为 x 和 y 的两个对应的 float 成员相等。(与 == 运算符不同,NaN 被认为等于其自身,并且 0.0f 不等于 -0.0f。)

如果 Double.valueOf(x).equals(Double.valueOf(y)) 为真,则认为值分别为 x 和 y 的两个对应的 double 成员相等。(与 == 运算符不同,NaN 被认为等于其自身,并且 0.0 不等于 -0.0。)

如果 x.equals(y) 为真,则认为值分别为 x 和 y 的两个对应的 String、Class、enum 或 annotation 类型的成员相等。(注意,此定义对于 annotation 类型的成员是递归的。)

对于适当重载的 Arrays.equals(long[], long[]),如果 Arrays.equals(x, y) 为真, 则认为两个对应的数组类型的成员 x 和 y 相等。

覆盖

类 Object 中的 equals

参数:

obj - 要与之比较的引用对象。

返回:

如果指定的对象表示在逻辑上等效于该接口的 annotation,则返回 true,否则返回 false

另请参见

Object.hashCode(), Hashtable

--------------------------------------------------------------------------------

hashCode

int hashCode()返回此 annotation 的哈希代码,具体说明如下:

一个 annotation 的哈希代码是其成员(包括那些带有默认值的成员)的哈希代码的和,具体说明如下: annotation 成员的哈希代码是成员值哈希代码的 XOR(它是 String.hashCode() 计算得到的成员名哈希代码的 127 倍),具体说明如下:

成员值的哈希代码取决于其类型:

基值 v 的哈希代码等于 WrapperType.valueOf(v).hashCode(),其中 WrapperType 是对应 v 的基本类型的包装器类型(Byte、Character、Double、Float、Integer、Long、Short 或 Boolean)。

string、enum、class 或 annotation 的成员值 I 的哈希代码 v 是通过调用 v.hashCode() 来计算的。(对于 annotation 成员值,这是一种递归定义。)

数组成员值的哈希代码是通过基于该值调用 Arrays.hashCode 的适当重载来计算的。(各种基本类型和对象引用类型分别对应一个重载。)

覆盖:

类 Object 中的 hashCode

返回:

此 annotation 的哈希代码。

另请参见:

Object.equals(java.lang.Object), Hashtable

--------------------------------------------------------------------------------

toString

String toString()返回此 annotation 的字符串表示形式。表示形式的细节取决于实现,但下面的情况是最常见的:

@com.acme.util.Name(first=Alfred, middle=E., last=Neuman)

覆盖:

类 Object 中的 toString

返回

此 annotation 的字符串表示形式

--------------------------------------------------------------------------------

annotationType

Class<? extends Annotation> annotationType()返回此 annotation 的注释类型。

随便看

 

百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。

 

Copyright © 2004-2023 Cnenc.net All Rights Reserved
更新时间:2025/2/7 16:28:38