Annotation(注解)

来源:互联网 发布:a星算法优化 编辑:程序博客网 时间:2024/06/03 19:27

1.准确覆写@Override

如果准确控制覆写要求,使用@Override,如果覆写不正确,则它提示错误

package com.wjx.sayHello;class Persons{@Override//The method tostring() of type Persons must override or implement a supertype method//如果写了@Override没有覆写toString()会提示上述问题public String tostring(){return "hello";}}public class TestDemo {public static void main(String[] args){System.out.println(new Persons());}}

2 .声明过期处理@Deprecated

一些程序类再新版本不希望继续使用,但是在老版本里面还得继续使用,不能删除,此时我们声明注解@Deprecated来表示,声明过的方法或者类不建议使用,但是使用不会报错。

package com.wjx.sayHello;class Persons{@Deprecatedpublic Persons(){}public Persons(String name){}@Deprecatedpublic void fun(){}}public class TestDemo {public static void main(String[] args){Persons p=new Persons();Persons p1=new Persons("李四");p.fun();}}
3. 压制警告@

当调用某些方法,定义方法对象等操作时候,程序出现警告时候的时候,你不想让开发工具提示警告可以使用压制警告来避免提示警告

package com.wjx.sayHello;class Persons<T>{@Deprecatedpublic Persons(){}public Persons(String name){}@Deprecatedpublic void fun(){}}public class TestDemo {@SuppressWarnings({ "rawtypes", "unused" })public static void main(String[] args){//p使用Object接受对象,由于persons为泛型 没声明对象类型会出现警告Persons p=new Persons();//p1为使用,会出现警告Persons p1=new Persons("李四");p.fun();}}