2025-02-10

瞭解Annotation 的作用

掌握系統內建的三個Annotation

Annotation:在JDK1.5之後增加的一個新特性,這種特性被稱為元數據特性,在JDK1.5 之後稱為註釋,即:使用註釋的方式加入一些程序的信息。

java.lang.annotation.Annotation 接口是所有的 Annotation 都必須實現的接口。

 

 

 

1、@Override 表示方法覆寫的正確性,例如現在有如下一段代碼:

view plaincopy to clipboardprint?class Person{ 
    public String getInfo(){        // 取得信息  
        return "這是一個Person類。" ; 
    } 
}; 
class Student extends Person{   // 繼承此類  
    public String getinfo(){    // 覆寫方法  
        return "這是一個Student類。" ; 
    } 
}; 
public class OverrideAnnotationDemo01{ 
    public static void main(String args[]){ 
        Person per = new Student() ; 
        System.out.println(per.getInfo()) ; // 輸出信息  
    } 
}; 
class Person{
 public String getInfo(){  // 取得信息
  return "這是一個Person類。" ;
 }
};
class Student extends Person{ // 繼承此類
 public String getinfo(){ // 覆寫方法
  return "這是一個Student類。" ;
 }
};
public class OverrideAnnotationDemo01{
 public static void main(String args[]){
  Person per = new Student() ;
  System.out.println(per.getInfo()) ; // 輸出信息
 }
};

此時,可能存在某種失誤,將方法名稱編寫錯誤。

view plaincopy to clipboardprint?class Person{ 
    public String getInfo(){        // 取得信息  
        return "這是一個Person類。" ; 
    } 
}; 
class Student extends Person{   // 繼承此類  
    @Override 
    public String getinfo(){    // 覆寫方法  
        return "這是一個Student類。" ; 
    } 
}; 
public class OverrideAnnotationDemo01{ 
    public static void main(String args[]){ 
        Person per = new Student() ; 
        System.out.println(per.getInfo()) ; // 輸出信息  
    } 
}; 
class Person{
 public String getInfo(){  // 取得信息
  return "這是一個Person類。" ;
 }
};
class Student extends Person{ // 繼承此類
 @Override
 public String getinfo(){ // 覆寫方法
  return "這是一個Student類。" ;
 }
};
public class OverrideAnnotationDemo01{
 public static void main(String args[]){
  Person per = new Student() ;
  System.out.println(per.getInfo()) ; // 輸出信息
 }
};使用Override 註釋可以保證程序正確的執行。

2、@Deprecated

使用@Deprecated 註釋的Annotation 本身是不建議使用的一個操作。

view plaincopy to clipboardprint?class Demo{ 
    @Deprecated         // 聲明不建議使用的操作  
    public String getInfo(){ 
        return "這是一個Person類。" ; 
    } 
}; 
public class DeprecatedAnnotationDemo01{ 
    public static void main(String args[]){ 
        Demo d = new Demo() ; 
        System.out.println(d.getInfo()) ; 
    } 
}; 
class Demo{
 @Deprecated   // 聲明不建議使用的操作
 public String getInfo(){
  return "這是一個Person類。" ;
 }
};
public class DeprecatedAnnotationDemo01{
 public static void main(String args[]){
  Demo d = new Demo() ;
  System.out.println(d.getInfo()) ;
 }
};以上程序讓編譯出錯,而是出現瞭一個安全的警告信息。

view plaincopy to clipboardprint?@Deprecated         // 聲明不建議使用的操作  
class Demo{ 
    public String getInfo(){ 
        return "這是一個Person類。" ; 
    } 
}; 
public class DeprecatedAnnotationDemo02{ 
    public static void main(String args[]){ 
        Demo d = new Demo() ; 
        System.out.println(d.getInfo()) ; 
    } 
}; 
@Deprecated   // 聲明不建議使用的操作
class Demo{
 public String getInfo(){
  return "這是一個Person類。" ;
 }
};
public class DeprecatedAnnotationDemo02{
 public static void main(String args[]){
  Demo d = new Demo() ;
  System.out.println(d.getInfo()) ;
 }
};3、@SuppressWarnings

用於壓制警告信息。

以之前的泛型操作為例,在泛型中如果沒有指定泛型類型,則使用時肯定出現安全警告。

view plaincopy to clipboardprint?class Demo<T>{ 
    private T var ; 
    public T getVar(){ 
        return this.var ; 
    } 
    public void setVar(T var){ 
        this.var = var ; 
    } 
}; 
public class SuppressWarningsAnnotationDemo01{ 
    public static void main(String args[]){ 
        Demo d = new Demo() ; 
        d.setVar("李興華") ; 
        System.out.println("內容:" + d.getVar()) ; 
    } 
}; 
class Demo<T>{
 private T var ;
 public T getVar(){
  return this.var ;
 }
 public void setVar(T var){
  this.var = var ;
 }
};
public class SuppressWarningsAnnotationDemo01{
 public static void main(String args[]){
  Demo d = new Demo() ;
  d.setVar("李興華") ;
  System.out.println("內容:" + d.getVar()) ;
 }
};此時就可以使用 SuppressWarnings 這個Annotation 將這種警告信息進行壓制。

view plaincopy to clipboardprint?class Demo<T>{ 
    private T var ; 
    public T getVar(){ 
        return this.var ; 
    } 
    public void setVar(T var){ 
        this.var = var ; 
    } 
}; 
public class SuppressWarningsAnnotationDemo01{ 
    @SuppressWarnings("unchecked") 
    public static void main(String args[]){ 
        Demo d = new Demo() ; 
        d.setVar("李興華") ; 
        System.out.println("內容:" + d.getVar()) ; 
    } 
}; 
class Demo<T>{
 private T var ;
 public T getVar(){
  return this.var ;
 }
 public void setVar(T var){
  this.var = var ;
 }
};
public class SuppressWarningsAnnotationDemo01{
 @SuppressWarnings("unchecked")
 public static void main(String args[]){
  Demo d = new Demo() ;
  d.setVar("李興華") ;
  System.out.println("內容:" + d.getVar()) ;
 }
};以上隻是壓制瞭一個警告信息,當然,也可以同時壓制多個警告信息,隻需要以數組的形式出現即可。

view plaincopy to clipboardprint?@Deprecated 
class Demo<T>{ 
    private T var ; 
    public T getVar(){ 
        return this.var ; 
    } 
    public void setVar(T var){ 
        this.var = var ; 
    } 
}; 
public class SuppressWarningsAnnotationDemo02{ 
    @SuppressWarnings({"unchecked","deprecation"}) 
    public static void main(String args[]){ 
        Demo d = new Demo() ; 
        d.setVar("李興華") ; 
        System.out.println("內容:" + d.getVar()) ; 
    } 
}; 
@Deprecated
class Demo<T>{
 private T var ;
 public T getVar(){
  return this.var ;
 }
 public void setVar(T var){
  this.var = var ;
 }
};
public class SuppressWarningsAnnotationDemo02{
 @SuppressWarnings({"unchecked","deprecation"})
 public static void main(String args[]){
  Demo d = new Demo() ;
  d.setVar("李興華") ;
  System.out.println("內容:" + d.getVar()) ;
 }
};


通過剛才發現 SuppressWarnings 註釋可以發現裡面是使用 value 的字符串數組接收的,所以,在傳入註釋參數的時候也可以明確的指出要傳給那個變量接收。

view plaincopy to clipboardprint?@Deprecated 
class Demo<T>{ 
    private T var ; 
    public T getVar(){ 
        return this.var ; 
    } 
    public void setVar(T var){ 
        this.var = var ; 
    } 
}; 
public class SuppressWarningsAnnotationDemo03{ 
    @SuppressWarnings(value={"unchecked","deprecation"}) 
    public static void main(String args[]){ 
        Demo d = new Demo() ; 
        d.setVar("李興華") ; 
        System.out.println("內容:" + d.getVar()) ; 
    } 
}; 
@Deprecated
class Demo<T>{
 private T var ;
 public T getVar(){
  return this.var ;
 }
 public void setVar(T var){
  this.var = var ;
 }
};
public class SuppressWarningsAnnotationDemo03{
 @SuppressWarnings(value={"unchecked","deprecation"})
 public static void main(String args[]){
  Demo d = new Demo() ;
  d.setVar("李興華") ;
  System.out.println("內容:" + d.getVar()) ;
 }
};總結:

1、系統內建的三個 Annotation 的作用,可以發現通過註釋可以完成一些代碼的其他功能。

作者“韓世雷 程序員專欄”

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *