---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IOS开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------
一、注解入门
1,注解理解
注解相当于一种标记,在程序中加了注解就等于为程序打上了某种标记,没加,则等于没有某种标记,以后,javac编译器,开发工具和其他程序可以用反射来了解你的类及各种元素上有无何种标记,看你有什么标记,就去干相应的事。标记可以加在包,类,字段,方法,方法的参数以及局部变量上。
2,java提供的几个基本注解
2.1,Override
表示一个方法声明打算重写超类中的另一个方法声明。如果方法利用此注释类型进行注解但没有重写超类方法,则编译器会生成一条错误消息。
@Override//为equals方法加上标记,说明要重写object中的equals方法
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
2.2,Deprecated
用 @Deprecated 注释的程序元素,不鼓励程序员使用这样的元素,通常是因为它很危险或存在更好的选择。在使用不被赞成的程序元素或在不被赞成的代码中执行重写时,编译器会发出警告。
@Deprecated
public void show(){
System.out.println("hello");
}//声明此类的show方法过时或存在危险,不建议使用
@SuppressWarnings("deprecation")
public static void main(String[] args) {
System.runFinalizersOnExit(true);
}//runFinalizersOnExit方法已过时,为了使编译器不提示警告在main方法上使用SuppressWarnings注解
三、元注解
1,元注解:对注解进行注解的注解成为元注解
2,java提供的两个元注解
2.1,Retention
指示注释类型的注释要保留多久。不存在 Retention 注释,则默认为RetentionPolicy.CLASS。
@Retention(RetentionPolicy.RUNTIME)
public @interface MetaAnnotationType {
...
}
//声明MetaAnnotationType注解生命期RetentionPolicy.RUNTIME,即为译器将把注释记录在类文件中,在运行时 VM 将保留注释,因此可以反射性地读取
2.2,Target
指示注释类型所适用的程序元素的种类。如果不存在 Target 元注释,则声明的类型可以用在任一程序元素上。
@Target(ElementType.METHOD)
public @interface MetaAnnotationType {
...
}
//声明MetaAnnotationType注解只能用于方法上
三、自定义注解及应用
1,注解的应用结构图
2,注解应用
2.1,自定义注解
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}//定义注解MyAnnotation格式与定义接口相似,前面加@
2.2,为类加注解并通过反射获取
通过Class类的isAnnotationPresent方法判断该类是否有注解,通过getAnnotation方法拿到注解
import java.lang.annotation.Annotation;
@MyAnnotation
public class AnotationTest {
public static void main(String[] args) {
MyAnnotation annotation = null;
if(AnotationTest.class.isAnnotationPresent(MyAnnotation.class)){
annotation = (MyAnnotation)AnotationTest.class.getAnnotation(MyAnnotation.class);
System.out.println(annotation);
}
}
}//打印@MyAnnotation()
四、为注解加属性
1,定义基本类型的属性并应用
为注解添加String类型的color属性,格式为String color();注意定义属性时与类中的方法相似,带括号
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String color();
}
2,获取注解属性
获取属性时,与对象调用方法类似annotation.color()
import java.lang.annotation.Annotation;
@MyAnnotation(color = "red")
public class AnotationTest {
public static void main(String[] args) {
MyAnnotation annotation = null;
if(AnotationTest.class.isAnnotationPresent(MyAnnotation.class)){
annotation = (MyAnnotation)AnotationTest.class.getAnnotation(MyAnnotation.class);
System.out.println(annotation.color());
}
}
}//打印:red
3,为属性设置缺省值
设置属性时使用default为注解设置缺省值,设置后应用注解是可以不设定属性值,使用默认值,也可以设置自定义属性值
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String color() default "blue";
}
4,value
当注解的属性名为value时,当注解中只有value属性需要设置属性值,可以省略value = ,
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String color() default "blue";
String value();
}
//设置属性名为value的属性
import java.lang.annotation.Annotation;
@MyAnnotation("hello")//设置时省略value=
public class AnotationTest {
public static void main(String[] args) {
MyAnnotation annotation = null;
if(AnotationTest.class.isAnnotationPresent(MyAnnotation.class)){
annotation = (MyAnnotation)AnotationTest.class.getAnnotation(MyAnnotation.class);
System.out.println(annotation.value());
}
}
}//打印:hello
五、注解的其它属性
1,数组属性
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String color() default "blue";
int[] array();
}
import java.lang.annotation.Annotation;
@MyAnnotation(array = {1,2,3})
public class AnotationTest {
public static void main(String[] args) {
MyAnnotation annotation = null;
if(AnotationTest.class.isAnnotationPresent(MyAnnotation.class)){
annotation = (MyAnnotation)AnotationTest.class.getAnnotation(MyAnnotation.class);
System.out.println(annotation.array().length);
}
}
}//打印:3,当数组只有一个元素时可以省略{},如array = {1}可写成array = 1
2,枚举型属性
2.1,定义一个枚举MyColor
public enum MyColor {
RED{
public MyColor nextColer() {
return BLUE;
}
},
BLUE{
public MyColor nextColer() {
return RED;
}
};
public abstract MyColor nextColer();
}
2.2,为注解加枚举属性
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String color() default "blue";
MyColor myColor();
}
2.3,使用并获取
import java.lang.annotation.Annotation;
@MyAnnotation(myColor=MyColor.BLUE)
public class AnotationTest {
public static void main(String[] args) {
MyAnnotation annotation = null;
if(AnotationTest.class.isAnnotationPresent(MyAnnotation.class)){
annotation = (MyAnnotation)AnotationTest.class.getAnnotation(MyAnnotation.class);
System.out.println(annotation.myColor().nextColer());
}
}
}
//打印:RED
3,注解型属性
3.1,定义一个注解
public @interface MyAnnotation2 {
String value();
}
3.2,为注解加上注解型属性
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String color() default "blue";
MyAnnotation2 ma2();
}
3.3,使用并获取
import java.lang.annotation.Annotation;
@MyAnnotation(ma2 = @MyAnnotation2("注解类型属性"))
public class AnotationTest {
public static void main(String[] args) {
MyAnnotation annotation = null;
if(AnotationTest.class.isAnnotationPresent(MyAnnotation.class)){
annotation = (MyAnnotation)AnotationTest.class.getAnnotation(MyAnnotation.class);
System.out.println(annotation.ma2().value());
}
}
}//打印:注解类型属性
---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IOS开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------
详细请查看:<a href="http://edu.csdn.net" target="blank">http://edu.csdn.net</a>