系统内建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
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
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
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
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
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
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
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
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 的作用,可以发现通过注释可以完成一些代码的其他功能。
作者“韩世雷 程序员专栏”