王朝网络
分享
 
 
 

Annotation简介

王朝other·作者佚名  2007-01-27
宽屏版  字体: |||超大  

对于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引入了Metedata(元数据)很容易的就能够调用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

@SuppressWarnings annotation 告诉编译器禁止别的元素产生的特殊的警告(warnings),在前面的例子里,useDeprecatedMethod调用了Animal的不赞成使用的一个方法。一般情况下,编译器会给出一个警告(warning),但是在这种情况下,不会产生这个警告,也就是说被suppress。

每个编译器的警告都属于一个类型。Java Language Specification列出了两种类型:"deprecation" 和 "unchecked"。"unchecked" warning 发生在使用非generic的旧代码交互的generic collection类时。为了禁止不止一种的警告时,使用下面的语法:

@SuppressWarnings({"unchecked", "deprecation"})

关于Annotation还可以参考下面的文章:

作者:cleverpig(作者的Blog:http://blog.matrix.org.cn/page/cleverpig)

原文: http://www.matrix.org.cn/resource/article/44/44048_Java+Annotation.html

http://www.matrix.org.cn/resource/article/44/44055_Java+Annotation+Reflect.html

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有