码迷,mamicode.com
首页 > 编程语言 > 详细

Java中三种常见的注释(注解) Annotation

时间:2017-01-14 23:05:36      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:ret   过期   rgs   注释   div   void   not   常见   不能   

Java为我们提供了三种Annotation方便我们开发。

1 Override-函数覆写注解

如果我们想覆写Object的toString()方法,请看下面的代码:

 1 class AnnotationDemo
 2 {
 3     private String info;
 4     public AnnotationDemo(String info)
 5     {
 6         this.info = info;
 7     }
 8     
 9     public String tostring()
10     {
11         return "info的值是:" + this.info;
12     }
13 }
14 
15 public class Main
16 {
17     public static void main(String[] args)
18     { 
19         AnnotationDemo ad = new AnnotationDemo("你好");
20         System.out.println(ad);
21         System.out.println("Main Done//~~");
22     } 
23 }

上面的代码,我们期望能在AnnotationDemo类中覆写toString()方法,结果在运行的时候发现,程序调用的是Object的toString方法。原因是,我们的函数代码编写有瑕疵,将本应是toString()的方法名写成了tostring().该bug在我们运行代码后才能暴露出来。如果我们在tostring()上加上@Override注解,就可以显示的告诉JAVA编译器,我的函数是要覆写父类方法,请执行检查。请看代码:

 1 package main;
 2  
 3 
 4 class AnnotationDemo
 5 {
 6     private String info;
 7     public AnnotationDemo(String info)
 8     {
 9         this.info = info;
10     }
11     
12     @Override
13     public String tostring()
14     {
15         return "info的值是:" + this.info;
16     }
17 }
18 
19 public class Main
20 {
21     public static void main(String[] args)
22     { 
23         AnnotationDemo ad = new AnnotationDemo("你好");
24         System.out.println(ad);
25         System.out.println("Main Done//~~");
26     } 
27 }

上面的代码不能通过编译。

2 Depreced-方法过期注解

如果我们在方法上用@Depreced注解,那么就是告诉用户,这个方法已经不推荐使用了。如下面的代码:

 1 class AnnotationDemo
 2 {
 3     private String info;
 4     public AnnotationDemo(String info)
 5     {
 6         this.info = info;
 7     }
 8     
 9     @Deprecated
10     public void showInfo()
11     {
12         System.out.println(this.info);
13     }
14     
15     @Override
16     public String toString()
17     {
18         return "info的值是:" + this.info;
19     }
20 }
21 
22 public class Main
23 {
24     public static void main(String[] args)
25     { 
26         AnnotationDemo ad = new AnnotationDemo("你好");
27         System.out.println(ad);
28         System.out.println("Main Done//~~");
29     } 
30 }

上面的代码在编译的时候会警告用户,showInfo()方法已经不推荐使用了。

 

3  @SuppressWarning-压制警告

压制警告的意思是,当我们代码有警告信息的时候,而我们不认为该警告会对我们的代码造成威胁,此时可以用@SuppressWarning将警告的提示信息取消。

 1 @SuppressWarnings("serial")
 2 class AnnotationDemo implements Serializable
 3 {
 4     private String info;
 5     public AnnotationDemo(String info)
 6     {
 7         this.info = info;
 8     }
 9     
10     @Deprecated
11     public void showInfo()
12     {
13         System.out.println(this.info);
14     }
15     
16     @Override
17     public String toString()
18     {
19         return "info的值是:" + this.info;
20     }
21 }

上面的代码实现了Serializable接口,该接口需要类中有一个serialVersionUID字段已标志不同的版本。而实际上我们不需要这个字段,那么在类上将该警告压制住,编译器就不会在提示警告信息了。

Java中三种常见的注释(注解) Annotation

标签:ret   过期   rgs   注释   div   void   not   常见   不能   

原文地址:http://www.cnblogs.com/kuillldan/p/6286191.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!