java注解annocation

更新时间:2023-10-17 08:04:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

java注解(Annotation)

(1)Override注解是子类实现父类方法的覆盖,其实现是@Override

public class Test1 {

@Override //产生了覆盖父类方法的注解 public String toString() {

return \; }

public String tostring() { return \; }

public static void main(String[] args) { Test1 t = new Test1();

System.out.println(t.toString());

System.out.println(t.tostring()); //此方法是新增的成员方法

System.out.println(t); //直接调用覆盖的toString方法

} }

(2)Deprecate注解表示方法是不建议使用的

public class Test3 { @Deprecated

public void dosomething() { // 产生了警告

System.out.println(\); }

public static void main(String[] args) { Test3 t = new Test3(); t.dosomething(); } }

(3)SuppressWarnings(\注解是表示抑制警告

public class Test2 {

@SuppressWarnings(\) //产生取消警告的注解 public static void main(String[] args) { Map map = new TreeMap(); map.put(\, new Date());

System.out.println(map.get(\));

} }

(4)在定义自定义的注解时,要用(public @interface 注解名)来定义,在定义其注解的属性值时,如果属性名是value,则在代码中直接使用@注解名(value),如果属性值不是value时,则应该用@注解名(name = value)来定义。

public @interface Annocation { // 子定义一个注解 String value(); }

public class UseAnnotation { @Annocation(value = \) // 使用自定义的注解

public UseAnnotation() {

System.out.println(\); }

@Annocation(\)

public void dosomething() {

System.out.println(\); }

public static void main(String[] args) {

new UseAnnotation();

new UseAnnotation().dosomething();

} }

本文来源:https://www.bwwdw.com/article/tcvf.html

Top