java中的注解
2015-01-31 12:38
113 查看
jdk1.5开始就增加了对元数据的支持,也就是注解(Annotation),这种标记可以在编译,类加载,运行的时候被读取。
1 有几种基本的注解
1) @Override(重写父类方法):只能用于方法,不能用于其他程序元素。用来指定方法覆盖。常常可以用来检查是否覆盖父类的方法
public class Erica {
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
return super.equals(obj);
}
}
把以上的方法改为,则会报错,因为父类没有如下方法。
@Override
public boolean equals(Erica obj) {
// TODO Auto-generated method stub
return super.equals(obj);
}
2) @Deprecated(标记已经过时):表示某个程序元素已过时,当其他程序使用已过时的类,方法时,编译器会发出警告.
3 @SuppressWarnings(抑制编译警告):作用于该程序元素中的所有子元素,取消显示的编译警告.平时用的很少。
二:修饰注释的元注解
1 定义一个注解,一般定义一个新的注解使用@interface关键字,与定义一个接口很像。
public @interface EricaAnnotation {
}
2编写一个类,使用其上的注解,并用反射打印出其使用的注解,看下图会发现其命令行啥也没有打印出来,明明用到了,注解却啥也没有打印出来,这是为什么呢?
这是因为可以用@Retention可以指定被修饰的注解保留多久
@RetentionPolicy.SOURCE:只保留在源码中
@RetentionPolicy.CLASS:编译器把注解保留在class中,运行时不在保留注解
@RetentionPolicy.RUNNTIME:class中有注解,运行时也有注解,程序可以通过反射得到注解信息。
下面是对应关系:
source - class -runtime
JAVA源文件-CLASS文件(jvm编译成class,class文件不是字节码)-内存中的字节码
把以上注解修改为如下,再次运行命令行就可以打印出注解信息了:
@Retention(RetentionPolicy.RUNTIME)
public @interface EricaAnnotation {
}
也可以使用@Target修饰注解只能使用于哪些元素:
如下:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface EricaAnnotation {
}
上面代码定义该注解只能使用于方法和类,接口或者枚举定义。
三:为注解增加属性
1 为注解增加基本属性
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface EricaAnnotation {
String color() default "blue";
}
使用注解的时候定义属性值
@EricaAnnotation(color="red")
public class AnnotationTest {
}
2 为注解增加数组属性
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface EricaAnnotation {
String color() default "blue";
int[]arrayAttr();
}
使用注解时的定义数组属性值:
@EricaAnnotation(color="red",arrayAttr={1,2,3})
public class AnnotationTest {
}
3 为注解新增枚举属性:
定义枚举:
public enum Season {
spring ,summer,autumn,winter
}
在注解上面使用枚举属性
public @interface EricaAnnotation {
String color() default "blue";
int[]arrayAttr();
Season season();//枚举
}
为注解的枚举属性赋值
@EricaAnnotation(color="red",arrayAttr={1,2,3},season=Season.summer)
public class AnnotationTest {
}
4 新增注解类型的属性
定义一个新的注解
public @interface MetaAnnotation {
String value();
}
新增一个注解类型的属性
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface EricaAnnotation {
String color() default "blue";
int[]arrayAttr();
Season season();
MetaAnnotation annotationAttr();//注解
}
为注解的枚举属性赋值
@EricaAnnotation(color="red",arrayAttr={1,2,3},season=Season.summer,annotationAttr=@MetaAnnotation("wl"))
public class AnnotationTest {
}
最后是这些新增的注解,运用反射打印出来的值:
1 有几种基本的注解
1) @Override(重写父类方法):只能用于方法,不能用于其他程序元素。用来指定方法覆盖。常常可以用来检查是否覆盖父类的方法
public class Erica {
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
return super.equals(obj);
}
}
把以上的方法改为,则会报错,因为父类没有如下方法。
@Override
public boolean equals(Erica obj) {
// TODO Auto-generated method stub
return super.equals(obj);
}
2) @Deprecated(标记已经过时):表示某个程序元素已过时,当其他程序使用已过时的类,方法时,编译器会发出警告.
3 @SuppressWarnings(抑制编译警告):作用于该程序元素中的所有子元素,取消显示的编译警告.平时用的很少。
二:修饰注释的元注解
1 定义一个注解,一般定义一个新的注解使用@interface关键字,与定义一个接口很像。
public @interface EricaAnnotation {
}
2编写一个类,使用其上的注解,并用反射打印出其使用的注解,看下图会发现其命令行啥也没有打印出来,明明用到了,注解却啥也没有打印出来,这是为什么呢?
这是因为可以用@Retention可以指定被修饰的注解保留多久
@RetentionPolicy.SOURCE:只保留在源码中
@RetentionPolicy.CLASS:编译器把注解保留在class中,运行时不在保留注解
@RetentionPolicy.RUNNTIME:class中有注解,运行时也有注解,程序可以通过反射得到注解信息。
下面是对应关系:
source - class -runtime
JAVA源文件-CLASS文件(jvm编译成class,class文件不是字节码)-内存中的字节码
把以上注解修改为如下,再次运行命令行就可以打印出注解信息了:
@Retention(RetentionPolicy.RUNTIME)
public @interface EricaAnnotation {
}
也可以使用@Target修饰注解只能使用于哪些元素:
如下:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface EricaAnnotation {
}
上面代码定义该注解只能使用于方法和类,接口或者枚举定义。
三:为注解增加属性
1 为注解增加基本属性
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface EricaAnnotation {
String color() default "blue";
}
使用注解的时候定义属性值
@EricaAnnotation(color="red")
public class AnnotationTest {
}
2 为注解增加数组属性
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface EricaAnnotation {
String color() default "blue";
int[]arrayAttr();
}
使用注解时的定义数组属性值:
@EricaAnnotation(color="red",arrayAttr={1,2,3})
public class AnnotationTest {
}
3 为注解新增枚举属性:
定义枚举:
public enum Season {
spring ,summer,autumn,winter
}
在注解上面使用枚举属性
public @interface EricaAnnotation {
String color() default "blue";
int[]arrayAttr();
Season season();//枚举
}
为注解的枚举属性赋值
@EricaAnnotation(color="red",arrayAttr={1,2,3},season=Season.summer)
public class AnnotationTest {
}
4 新增注解类型的属性
定义一个新的注解
public @interface MetaAnnotation {
String value();
}
新增一个注解类型的属性
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface EricaAnnotation {
String color() default "blue";
int[]arrayAttr();
Season season();
MetaAnnotation annotationAttr();//注解
}
为注解的枚举属性赋值
@EricaAnnotation(color="red",arrayAttr={1,2,3},season=Season.summer,annotationAttr=@MetaAnnotation("wl"))
public class AnnotationTest {
}
最后是这些新增的注解,运用反射打印出来的值:
相关文章推荐
- SCA的java注解和API规范v100
- 在EasyDBO中使用Java注解配置映射的原理及使用
- Java的String经典笔试题付注解
- java之注解(一)
- The Java EE 5Tutorial-Preface(注解)
- java的注解
- 在EasyDBO中使用Java注解配置映射的原理及使用
- java5.0使用annotion注解
- Java注解(Tiger)
- EasyDBO中Java注解配置映射的原理及使用
- 在EasyDBO中使用Java注解配置映射的原理及使用
- 在EasyDBO中使用Java注解配置映射的原理及使用
- Java的String经典笔试题付注解
- 在EasyDBO中使用Java注解配置映射的原理及使用
- EasyDBO中Java注解配置映射的原理及使用
- java annocation(注解)
- 在EasyDBO中使用Java注解配置映射的原理及使用
- 管中窥豹Java5的新特性-注解(@Override)_Thinking in Java(4)_读书笔记(3)
- JAVA Annotation 注解
- java注解