1、@Override 强制检查子类的方法重写
 
java 代码
- package com.test;   
-   
- public class OverrideTest {   
-        
-     @Override  
-     public String toString()   
-     {   
-         return "This is override";   
-     }   
-   
-     public static void main(String[] args) {   
-            
-         OverrideTest ot = new OverrideTest();   
-         System.out.println(ot.toString());   
-     }   
-   
- }  
 
如下如果不小心把toString()写成了ToString(),则会通不过编译
java 代码
- package com.test;   
-   
- public class OverrideTest {   
-        
-     @Override  
-     public String ToString()   
-     {   
-         return "This is override";   
-     }   
-   
-     public static void main(String[] args) {   
-            
-         OverrideTest ot = new OverrideTest();   
-         System.out.println(ot.toString());   
-     }   
-   
- }   
 
2、@Deprecated 过时的,不建议被使用的
java 代码
- package com.test;   
-   
- import java.util.Date;   
-   
- public class DeprecatedTest {   
-   
-     @Deprecated  
-     public void doSomething()   
-     {   
-         System.out.println("do something");   
-     }   
-     public static void main(String[] args) {   
-         DeprecatedTest dt = new DeprecatedTest();   
-         dt.doSomething();   
-         Date date = new Date();   
-         date.toLocaleString();   
-     }   
-   
- }   
此时,第14行和第16行都会被划上线条,表示doSomething方法和toLocalString方法不建议被使用 。并且第16行前端有个警告符号。过时的或不建议被使用的方法被调用时是否出现警告,需要在IDE中设置:
window->preferences->java->compiler->Errors/Warnings->Deprecated and restricted API
将其中的两个复选框选中即可。
java 代码
- package com.test;   
-   
- public class SubDeprecatedTest extends DeprecatedTest {   
-   
-     @Override  
-     public void doSomething()   
-     {   
-         System.out.println("do something in subscribe class");   
-     }   
-     public static void main(String[] args) {   
-         SubDeprecatedTest sdt = new SubDeprecatedTest();   
-         sdt.doSomething();   
-     }   
-   
- }   
其中第6行出现警告符号
3、@SuppressWarnings 压制某些不必要的警告,压制一个或多个警告
语法: @SuppressWarnings("unchecked") 或者 @SuppressWarnings ({"unchecked","deprecation"})
 
java 代码
- package com.test;   
-   
- import java.util.Date;   
- import java.util.Map;   
- import java.util.TreeMap;   
-   
- public class SuppressWarningsTest {   
-   
-     @SuppressWarnings("unchecked")   
-     public static void main(String[] args) {   
-           
-         Map map = new TreeMap();   
-         map.put("hello", new Date());   
-            
-         System.out.println(map.get("hello"));   
-     }   
-   
- }   
 
java 代码
- package com.test;   
-   
- import java.util.Date;   
- import java.util.Map;   
- import java.util.TreeMap;   
-   
- public class SuppressWarningsTest {   
-   
-     @SuppressWarnings({"unchecked","deprecation"})   
-     public static void main(String[] args) {   
-           
-         Map map = new TreeMap();   
-         map.put("hello", new Date());   
-            
-         System.out.println(map.get("hello"));   
-           
-         DeprecatedTest dt = new DeprecatedTest();   
-         dt.doSomething();   
-     }   
-   
- }