当前位置:编程学习 > JAVA >>

Annotation _系统内建Annotation

了解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 这

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,