java注解

基本注解

  • @Override:表示重写父类方法
  • @Deprecated:表示某些方法已过时
  • @SuppressWarnings:取消显示指定的编辑器警告
  • @Documented: 指定某个注解类可以被写进文档
  • @Inherited:指定被它修饰的注解将具有继承性
  • @Target:同样只能修饰注解,表示该注解只能用于修饰某些程序单元
ElementType.ANNOTATION_TYPE:指定该注解只能修饰注解
ElementType.CONSTRUCTOR:只能修饰构造器
ElementType.FIELD:只能修饰成员变量
ElementType.LOCAL_VARIABLE:只能修饰局部变量
ElementType.METHOD:只能修饰方法定义
ElementType.PACKAGE:只能修饰包定义
ElementType.PARAMETER:只能修饰参数
ElementType.TYPE:只能修饰类、接口、枚举类型

自定义注解

自定义的注解要决定其使用范围,必须使用@Retention指定,Retention指定的范围由RetentionPolicy决定

所表示的范围
public static final RetentionPolicy SOURCE 在java源文件中存在
public static final RetentionPolicy CLASS 在java生成的class中存在
public static final RetentionPolicy RUNTIME 在java运行期存在

注解要起作用,必须结合反射

public enum Color {
    RED,
    GREEN,
    BLUE;
}

package myannotation;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * 自定义的注解
 */
//定义注解的作用范围
@Retention(value = RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    public String name();//自定义变量,在使用时需要先为变量赋值
    public int age() default 18;//设置默认值
    public String[] like();//数组变量
    public Color color();//枚举变量
}

package myannotation;

import java.util.Arrays;

/**
 * 为注解的变量赋值
 */
@MyAnnotation(name = "xiaoming",like = {"abc","def","xyz"},color = Color.BLUE)
public class Cat {
    private String name;
    private int age;
    private Color color;
    private String[] like;

    public Cat() {
    }

    public Cat(String name, int age, Color color, String[] like) {
        this.name = name;
        this.age = age;
        this.color = color;
        this.like = like;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public String[] getLike() {
        return like;
    }

    public void setLike(String[] like) {
        this.like = like;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color=" + color +
                ", like=" + Arrays.toString(like) +
                '}';
    }
}

package myannotation;

import java.lang.reflect.InvocationTargetException;

/**
 * 通过注解获取信息
 */
public class AnnotationDemo {
    public static void main(String[] args){
        Class<Cat> catClass = Cat.class;
        //获取指定类上的注解
        MyAnnotation annotation = catClass.getAnnotation(MyAnnotation.class);
        //获取注解上的变量值
        String name = annotation.name();
        Color color = annotation.color();
        int age = annotation.age();
        String[] like = annotation.like();
        try {
            Cat cat = catClass.getDeclaredConstructor().newInstance();
            cat.setAge(age);
            cat.setColor(color);
            cat.setLike(like);
            cat.setName(name);
            System.out.println(cat.toString());
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
}