注解

@Override 准确覆写

当子类覆写父类方法时,此注解检查覆写的方法是否正确声明,只有满足覆写方法要求才会编译通过,否则编译报错。

class Person {
    @Override
    public String toString(){
        return "hello world";
    }
}
public class MyEnum {
    public static void main(String[] args) {
        Person per = new Person();
        System.out.println(per);
    }
}

@Deprecated 过期声明

建议用户不使用原有的类&方法时,可以在类或方法上 @Deprecated 表示在当前版本中不推荐使用。

class Person {
    private String name;
    @Deprecated
    public Person(){
        System.out.println("不希望使用");
    }
    public Person(String name){
        this.name = name;
        System.out.println(this.name+"希望使用");
    }
}
public class MyEnum {
    public static void main(String[] args) {
        Person per = new Person();
        Person per1 = new Person("Java");
    }
}

@SuppressWarings 压制警告

调用某些操作可能产生问题的时候会出现警告信息,但是警告信息并不是异常。

class Person<T> {

}
public class MyEnum {
    public static void main(String[] args) {
        @SuppressWarnings({"rawtypes","unused"})
       Person<String> person = new Person<>();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章