@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<>();
}
}