`

使用JDK内建Annotation

阅读更多

1、Override 强制检查子类的方法重写

 

java 代码
  1. package com.test;   
  2.   
  3. public class OverrideTest {   
  4.        
  5.     @Override  
  6.     public String toString()   
  7.     {   
  8.         return "This is override";   
  9.     }   
  10.   
  11.     public static void main(String[] args) {   
  12.            
  13.         OverrideTest ot = new OverrideTest();   
  14.         System.out.println(ot.toString());   
  15.     }   
  16.   
  17. }  

 

如下如果不小心把toString()写成了ToString(),则会通不过编译

java 代码
  1. package com.test;   
  2.   
  3. public class OverrideTest {   
  4.        
  5.     @Override  
  6.     public String ToString()   
  7.     {   
  8.         return "This is override";   
  9.     }   
  10.   
  11.     public static void main(String[] args) {   
  12.            
  13.         OverrideTest ot = new OverrideTest();   
  14.         System.out.println(ot.toString());   
  15.     }   
  16.   
  17. }   

 

2、Deprecated 过时的,不建议被使用的

java 代码
  1. package com.test;   
  2.   
  3. import java.util.Date;   
  4.   
  5. public class DeprecatedTest {   
  6.   
  7.     @Deprecated  
  8.     public void doSomething()   
  9.     {   
  10.         System.out.println("do something");   
  11.     }   
  12.     public static void main(String[] args) {   
  13.         DeprecatedTest dt = new DeprecatedTest();   
  14.         dt.doSomething();   
  15.         Date date = new Date();   
  16.         date.toLocaleString();   
  17.     }   
  18.   
  19. }   

此时,第14行和第16行都会被划上线条,表示doSomething方法和toLocalString方法不建议被使用 。并且第16行前端有个警告符号。过时的或不建议被使用的方法被调用时是否出现警告,需要在IDE中设置:

window->preferences->java->compiler->Errors/Warnings->Deprecated and restricted API

将其中的两个复选框选中即可。

java 代码
  1. package com.test;   
  2.   
  3. public class SubDeprecatedTest extends DeprecatedTest {   
  4.   
  5.     @Override  
  6.     public void doSomething()   
  7.     {   
  8.         System.out.println("do something in subscribe class");   
  9.     }   
  10.     public static void main(String[] args) {   
  11.         SubDeprecatedTest sdt = new SubDeprecatedTest();   
  12.         sdt.doSomething();   
  13.     }   
  14.   
  15. }   

其中第6行出现警告符号

3、SuppressWarnings 压制某些不必要的警告,压制一个或多个警告

语法: @SuppressWarnings("unchecked") 或者 @SuppressWarnings ({"unchecked","deprecation"})

 

java 代码
  1. package com.test;   
  2.   
  3. import java.util.Date;   
  4. import java.util.Map;   
  5. import java.util.TreeMap;   
  6.   
  7. public class SuppressWarningsTest {   
  8.   
  9.     @SuppressWarnings("unchecked")   
  10.     public static void main(String[] args) {   
  11.         //当在JDK5的环境下编译时,如果不用@SuppressWarnings("unchecked") 这个Annotation,那么下面两行将会出现警告符号   
  12.         Map map = new TreeMap();   
  13.         map.put("hello"new Date());   
  14.            
  15.         System.out.println(map.get("hello"));   
  16.     }   
  17.   
  18. }   

 

java 代码
  1. package com.test;   
  2.   
  3. import java.util.Date;   
  4. import java.util.Map;   
  5. import java.util.TreeMap;   
  6.   
  7. public class SuppressWarningsTest {   
  8.   
  9.     @SuppressWarnings({"unchecked","deprecation"})   
  10.     public static void main(String[] args) {   
  11.         //当在JDK5的环境下编译时,如果不用@SuppressWarnings({"unchecked","deprecation"}) 这个Annotation,那么下面两行将会出现警告符号   
  12.         Map map = new TreeMap();   
  13.         map.put("hello"new Date());   
  14.            
  15.         System.out.println(map.get("hello"));   
  16.         //如果不用@SuppressWarnings({"unchecked","deprecation"}),则将会在dt.doSomething();所在行出现警告符号   
  17.         DeprecatedTest dt = new DeprecatedTest();   
  18.         dt.doSomething();   
  19.     }   
  20.   
  21. }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics