标签:注解 annotation deprecated 自定义注解 元注解
public class AnnotationTest {
@SuppressWarnings("deprecation")//deprecation表示废弃,此行表示不对废弃的方法警告
public static void main(String[] args) {
System.runFinalizersOnExit(true);
}
}上面代码片中的第2行,表示不对废弃的方法进行警告。runFinalizersOnExit()是废弃的方法(表示方法已过时,但为了兼容老代码而保留),不加注解情况下,在MyEclipse中会在方法名字上显示中划线,在调用方法的行上会显示警告图标。用jdk的javac编译,会给出警告。但是加了@SuppressWarings注解后,MyEclipse会去掉中划线,javac编译后也不会显示警告。public class AnnotationTest {
public static void main(String[] args) {
sayHello();
}
@Deprecated//Deprecated单词表示被废弃的,此行代码表示sayHello方法已被废弃
public static void sayHello(){
System.out.println("Hello world!");
}
}
public class ReflectPoint {
int x;
int y;
public ReflectPoint(int x,int y) {
this.x=x;
this.y=y;
System.out.println(this.hashCode());
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ReflectPoint other = (ReflectPoint) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}上面的代码中@Overrider表示紧接着的方法是重写父类的方法,相当于告诉编译器,如果下面的方法不是重写父类的方法,请报错,这样 public @interface MyAnnotation {
}可见@interface就是定义注解的关键词。由此可见注解时一种特殊的接口。@MyAnnotation
public class AnnotationTest {
public static void main(String[] args) {
//检查AnnotationTest类上是否有MyAnnotation注解
if(AnnotationTest.class.isAnnotationPresent(MyAnnotation.class)){
//获取AnnotationTest类的MyAnnotation注解
MyAnnotation annotation=(MyAnnotation)AnnotationTest.class.getAnnotation(MyAnnotation.class);
System.out.println(annotation);
}
}
}上面的代码运行后什么也不会输出,别急,因为出现这种情况是因为注解缺少东西。import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}然后再运行下面的反射代码:@MyAnnotation
public class AnnotationTest {
public static void main(String[] args) {
//检查AnnotationTest类上是否有MyAnnotation注解
if(AnnotationTest.class.isAnnotationPresent(MyAnnotation.class)){
//获取AnnotationTest类的MyAnnotation注解
MyAnnotation annotation=(MyAnnotation)AnnotationTest.class.getAnnotation(MyAnnotation.class);
System.out.println(annotation);
}
}
}输出结果@cn.itcase.day2.MyAnnotation(),其中cn.itcase.day2是包名import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
}如果需要给Target加多个参数则需这么写import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface MyAnnotation {
String color();
}上面的代码为MyAnnotation注解增加了一个属性color,这样就可以在调用注解的地方制定这个属性。如下:@MyAnnotation(color="blue")
public class AnnotationTest {
public static void main(String[] args) {
//检查AnnotationTest类上是否有MyAnnotation注解
if(AnnotationTest.class.isAnnotationPresent(MyAnnotation.class)){
//获取AnnotationTest类的MyAnnotation注解
MyAnnotation annotation=(MyAnnotation)AnnotationTest.class.getAnnotation(MyAnnotation.class);
System.out.println(annotation);
System.out.println(annotation.color());
}
}
}还可以为注解制定默认值,如下:public @interface MyAnnotation {
String color() default "blue";
}这样调用注解时,就可以不写参数,如下:public @interface MyAnnotation {
String color() default "blue";
String value();
}这时就可以这样调用注解public @interface MyAnnotation {
String color() default "blue";
String value();
int[] arrayAttr() default {1,2,3};
}调用方法如下:@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface MyAnnotation {
EnumDemo.TrafficLamp lamp() default EnumDemo.TrafficLamp.GREEN;
}其中的枚举EnumDemo.TrafficLamp源码如下:public class EnumDemo {
/**
* 交通灯枚举
* @author programBoy
*/
public enum TrafficLamp{
GREEN{
public TrafficLamp getNext() {
return YELLOW;
}
},
RED{
public TrafficLamp getNext() {
return GREEN;
}
},
YELLOW{
public TrafficLamp getNext() {
return RED;
}
};
public abstract TrafficLamp getNext();
}
}然后调用注解,并反射注解,进行测试,代码如下:@MyAnnotation
public class AnnotationTest {
public static void main(String[] args) {
//检查AnnotationTest类上是否有MyAnnotation注解
if(AnnotationTest.class.isAnnotationPresent(MyAnnotation.class)){
//获取AnnotationTest类的MyAnnotation注解
MyAnnotation annotation=(MyAnnotation)AnnotationTest.class.getAnnotation(MyAnnotation.class);
System.out.println(annotation);
System.out.println(annotation.lamp().getNext().name());
}
}
}由于注解的属性lamp有默认值,所以此处不用写此参数。运行上面的代码,结果如下:首先再定义一个注解,如下:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface AttrAnnotation {//注意此处必须加public,否则反射不出来
String value();
}@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface MyAnnotation {
AttrAnnotation annotationAttribute() default @AttrAnnotation("Hello"); //当注解被当做类型使用时,不需要加@符号,当注解需要被实例化时,才需要加@符号。
}然后调用MyAnnotation注解,并反射其中的注解属性:
@MyAnnotation(annotationAttribute=@AttrAnnotation("雷军"))
public class AnnotationTest {
public static void main(String[] args) {
//检查AnnotationTest类上是否有MyAnnotation注解
if(AnnotationTest.class.isAnnotationPresent(MyAnnotation.class)){
//获取AnnotationTest类的MyAnnotation注解
MyAnnotation annotation=(MyAnnotation)AnnotationTest.class.getAnnotation(MyAnnotation.class);
System.out.println(annotation);
System.out.println(annotation.annotationAttribute().value());
}
}
}该程序运行结果如下:
@cn.itcase.day2.MyAnnotation(annotationAttribute=@cn.itcase.day2.AttrAnnotation(value=雷军))
雷军
标签:注解 annotation deprecated 自定义注解 元注解
原文地址:http://blog.csdn.net/joven0/article/details/46272421