Annotation _系统内建Annotation (二)

2014-11-24 02:43:01 · 作者: · 浏览: 1

};
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 的作用,可以发现通过注释可以完成一些代码的其他功能。

作者“韩世雷 程序员专栏”