学习实战全笔记--JavaSE--Annotation(注释)--5种基本注释的用法示例(JDK8)

来源:互联网 发布:stage淘宝网店 编辑:程序博客网 时间:2024/06/06 22:34

      Annotation(注解)即是Java供给了一种元程序中的元素相关 任何信息和着任何元数据(metadata)的途径和办法。Annotion(注解)是一个接口,程序能够通过反射 来获取指定程序元素的Annotion目标,然后通过Annotion目标来获取注解里边的元数据。

   基本的 Annotationjava.lang包内,其中共有5个基本的注释接口:
   Annotation Types
   1.Deprecated
   2.FunctionalInterface
   3.Override
   4.SafeVarargs
   5.SuppressWarnings
   下面是关于这五个基本注释的用法的详细解释与源代码,读者可以直接粘贴然后仔细阅读分析,运行并查看效果:

   (请使用不低于JDK1.8的版本编译运行)

<span style="font-size:14px;"><strong>/*下面的类测试了Java常用的5个基本Annotation*/import java.util.*;@SuppressWarnings(value="unchecked")public class AnnotationTest extends Super{/*1.@Override 用来指定方法重载,它可以强制子类必须覆盖父类的方法。@Override只能修饰方法。好处:防止写错或者忘记覆盖父类的方法,起到很好的警示作用*/@Override//public void infosuper()/*上面不小心把infoSuper()方法的签名写错了,就会提示下面的错误:--------------------------------------------------------------------------------------------------AnnotationTest.java:11: 错误: 方法不会覆盖或实现超类型的方法        @Override        ^1 个错误--------------------------------------------------------------------------------------------------如果不加@Override注释的话,就不会有任何提示的*/public void infoSuper(){System.out.println("I am the AnnotationTest Class.");}/*2.@Deprecated 用于表示某个程序元素(类、接口、方法等)已过时当其他程序使用时,会在编译时发出警告。@Deprecated 与文档注释 @deprecated基本相同,只是使用的场合不同。下面方法重写了父类的被@Deprecated标记的infoDeprecated()方法,编译时会出现如下警告:--------------------------------------------------------------------------------------------------注: AnnotationTest.java使用或覆盖了已过时的 API。注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。--------------------------------------------------------------------------------------------------但是执行并不受影响。*/@Overridepublic void infoDeprecated(){System.out.println("I am a Deprecated method.");}/*3.@SuppressWarnings 指示被该Annotation修饰的程序元素以及该程序元素中的所有子元素取消显示指定的编译器警告*///下面的类:CollectionDemo就测试了这个注释/*4.@SafeVarargs 抑制"堆污染"警告*///这里用到了可变长参数列表:Varargs ,其参数个数可变,相当于一个参数数组public static void infoVarargs(List<String>...list){List li=new ArrayList<Integer>();li.add(20);List<String> li2=li;/*下面会引起如下Runtime异常 * Exception in thread "main" java.lang.ClassCastException:  * java.lang.Integer cannot be cast to java.lang.String*///System.out.println(li2.get(0));/*Java将这种错误的原因称为"堆污染"(Heap pollution),将加泛型与不加泛型的元素之间相互赋值经常会引发"堆污染" * 由于java不允许泛型数组,可变长参数列表很容易引起"堆污染" * */List[] listArray=list;}/*5.@FunctionnalInterface如果一个接口只包含一个抽象方法(可以有多个默认方法或多个static方法),那么这个接口就叫函数式接口。*/public static void main(String[] args){new AnnotationTest().infoSuper();new AnnotationTest().infoDeprecated();new CollectionDemo().infoWaring();infoVarargs();}}//超类class Super{@Deprecatedpublic void infoDeprecated(){System.out.println("I am a Deprecated method.");}public void infoSuper(){System.out.println("I am the Super Class.");}}//关闭整个类的编译器警告@SuppressWarnings(value="unchecked")class CollectionDemo{public void infoWaring(){//集合没有泛型在笔者的EclipseSDK中将会有以下警告产生://Map is a raw type. References to generic type Map<K,V> should be parameterized//但是添加了@SuppressWarnings(value="unchecked")就没有了HashMap map=new HashMap();map.put("one",1);System.out.println(map);}}//函数式接口@FunctionalInterfaceinterface FunIn{static void method(){System.out.println("默认类方法");}default void staticMethod(){System.out.println("默认类方法");}/*下面test2()方法与test()方法同时存在于函数式接口,编译时将出现如下错误警告:AnnotationTest.java:110: 错误: 意外的 @FunctionalInterface 注释@FunctionalInterface^FunIn 不是函数接口    在 接口 FunIn 中找到多个非覆盖抽象方法*///void test2();void test();}</strong></span>

 


0 0
原创粉丝点击