java注解

java 注解的有关知识

一. 基本的 Annotation
使用 Annotation 时要在其前面增加 @符号, 并把该 Annotation 当成一个修饰符使用。用于修饰它支持的程序元素
三个基本的 Annotation:
@Override: 限定重写父类方法, 该注释只能用于方法
@Deprecated: 用于表示某个程序元素 (类, 方法等) 已过时
@SuppressWarnings: 抑制编译器警告
例:
import java.util.ArrayList;
import java.util.List;
/*
 * 注解
 * 1.JDK 提供的常用的注解
 *   @Override: 限定重写父类方法,该注释只能用于方法
 *   @Deprecated: 用于表示某个程序元素(类,方法等)已过时
 *   @SupressWarnings: 抑制编译器警告
 * 2. 如何自定义一个注释
 * 3. 元注解
 * */
public class TestAnnotation {
    public static void main(String[] args){
        Person person=new Student("tom",21);
        person.walk();
        person.eat();
    @SuppressWarnings({ </span>"unused", "rawtypes"<span style="color: rgba(0, 0, 0, 1)"> })
    List list</span>=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ArrayList();

    @SuppressWarnings(</span>"unused"<span style="color: rgba(0, 0, 0, 1)">)
    </span><span style="color: rgba(0, 0, 255, 1)">int</span> i=10<span style="color: rgba(0, 0, 0, 1)">;  
}

}
class Student extends Person{
public Student(String name, int age) {
super(name, age);
}
@Override
public void walk(){
System.out.println(
"学生走路");
}
@Override
public void eat(){
System.out.println(
"学生吃饭");
}
}
@Deprecated
class Person{
private String name;
private int age;

</span><span style="color: rgba(0, 0, 255, 1)">public</span> Person(String name, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> age) {
    </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">();
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name;
    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.age =<span style="color: rgba(0, 0, 0, 1)"> age;
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> walk(){
    System.out.println(</span>"走路"<span style="color: rgba(0, 0, 0, 1)">);
}
@Deprecated
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> eat(){
    System.out.println(</span>"吃饭"<span style="color: rgba(0, 0, 0, 1)">);
}
@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String toString(){
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> "Person [name="+name+",age="+age+"]"<span style="color: rgba(0, 0, 0, 1)">;
}

}

二. 自定义 Annotation
1. 定义新的 Annotation 类型使用 @interface 关键字
2.Annotation 的成员变量在 Annotation 定义中以无参数方法的形式来声明. 其方法名和返回值定义了该成员的名字和类型. 
3. 可以在定义 Annotation 的成员变量时为其指定初始值, 指定成员变量的初始值可使用 default 关键字
   public @interface MyAnnotation{
       String name() default “hello";
        }
4. 没有成员定义的 Annotation 称为标记; 包含成员变量的 Annotation 称为元数据 Annotation
例:
//自定义的注解
public @interface MyAnnotation {String value() default "hello";
}

import java.util.ArrayList; import java.util.List; /* * 注解 * 1.JDK 提供的常用的注解 * @Override: 限定重写父类方法,该注释只能用于方法 * @Deprecated: 用于表示某个程序元素(类,方法等)已过时 * @SupressWarnings: 抑制编译器警告 * 2. 如何自定义一个注释 * 3. 元注解 * */ public class TestAnnotation { public static void main(String[] args){ Person person=new Student("tom",21); person.walk(); person.eat();
    @SuppressWarnings({ </span>"unused", "rawtypes"<span style="color: rgba(0, 0, 0, 1)"> })
    List list</span>=<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ArrayList();
    
    @SuppressWarnings(</span>"unused"<span style="color: rgba(0, 0, 0, 1)">)
    </span><span style="color: rgba(0, 0, 255, 1)">int</span> i=10<span style="color: rgba(0, 0, 0, 1)">;
}

}
@MyAnnotation(value="atguigu")
class Student extends Person{
public Student(String name, int age) {
super(name, age);
}
@Override
public void walk(){
System.out.println(
"学生走路");
}
@Override
public void eat(){
System.out.println(
"学生吃饭");
}
}
@Deprecated
class Person{
private String name;
private int age;
@MyAnnotation(value
="atguigu")
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
@MyAnnotation(value
="atguigu")
public void walk(){
System.out.println(
"走路");
}
@Deprecated
public void eat(){
System.out.println(
"吃饭");
}
@Override
public String toString(){
return "Person [name="+name+",age="+age+"]";
}
}

三. 元注解
JDK 的元 Annotation 用于修饰其他 Annotation 定义
JDK5.0 提供了专门在注解上的注解类型,分别是:Retention、Target、Documented、Inherited
@Retention:只能用于修饰一个 Annotation 定义, 用于指定该 Annotation 可以保留多长时间,必须为该成员变量 RetentionPolicy 指定值:
        RetentionPolicy.SOURCE: 编译器直接丢弃这种策略的注释
        RetentionPolicy.CLASS: 编译器将把注释记录在 class 文件中. 当运行 Java 程序时, JVM 不会保留注解。 这是默认值
        RetentionPolicy.RUNTIME: 编译器将把注释记录在 class 文件中. 当运行 Java 程序时, JVM 会保留注释. 程序可以通过反射获取该注释
@Target: 用于修饰 Annotation 定义, 用于指定被修饰的 Annotation 能用于修饰哪些程序元素. @Target 也包含一个名为 value 的成员变量.
 
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
//自定义的注解
@Target({TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {String value() default "hello";
}