一、注解

注解(Annotation)是从JDK5.0开始引入,以“@注解名”在代码中存在。

Annotation 可以像修饰符一样被使用,可用于修饰包、类、构造器、方法、成员变量、参数、局部变量的声明。还可以添加一些参数值,这些信息被保存在 Annotation 的 “name=value” 对中。

注解可以在类编译、运行时进行加载,体现不同的功能。

二、三个基本注解 

@Override:该注解只能用于方法,校验是否重写

 @Deprecated:用于表示被标记的数据已经过时,不推荐使用。

 @SuppressWarnings:抑制编译警告

public class AnnotationTest {
    public static void main(String[] args) {
        Person person = new Person();
        person.method();
    }
}
class Person{
    String name;
    int age;
    public void eat(){
        System.out.println("父类正在吃饭");
    }
    @Deprecated
    public void method(){
        System.out.println("该方法过时");
    }
}
class Student extends Person{
    @Override
    public void eat() {
        System.out.println("子类继承了吃饭");
    }
}

 三、自定义注解

public @interface MyAnnotation {
    String value() default "class";
}

 

08-26 12:32