Annotation注解(1.5新特性)

    技术2026-05-26  12

    Annotation注解(1.5新特性)注解就相当于一个你的源程序中要调用一个类,要在源程序中应用某个注解,得先准备好了这个注解类。就像你要调用某个类,得先有开发好这个类。@SuppressWarnings("deprecation")//压制警告@Deprecated 过时的  @Override 覆盖

    标记可以加在包,类,字段,方法,局部变量上,可以加多个

    package ZHANG.Annotation; @ZhangAnnotation(color="red",value="abc",arrayAttr=1) //属性color值为red public class AnnotationTest { @SuppressWarnings("deprecation") @ZhangAnnotation("xyz") public static void main(String[] args) { //这个类上是否有注解 if(AnnotationTest.class.isAnnotationPresent(ZhangAnnotation.class)){ ZhangAnnotation annotation = AnnotationTest.class.getAnnotation(ZhangAnnotation.class); w(annotation.color()); w(annotation.value()); w(annotation.arrayAttr().length); } } public static void w(Object o){ System.out.println(o); } }

    Annotation

    package ZHANG.Annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; //元注解. 在注解上加注解 @Retention(RetentionPolicy.RUNTIME) //@Target(ElementType.METHOD) public @interface ZhangAnnotation { String color() default "blue";//属性 String value(); int[] arrayAttr() default {3,2,3};//数组 }  

     1.注释类 @interface A{}2.应用了"注释类"的类 @AClass B{}3.对"应用了注解类的类"进行反射操作的类Class C{   B.class.isAnnotationPresent(A.class);   A a = B.class.getAnnotation(A.class); }JAVA SE5.0 的新特性:枚举,自动装箱,拆箱,foreach,可变参数,静态导入,泛型。lang包中1.Annotation:  内建了三个:---->@Override ,@Deprecated,@suppressWarnings@Override --->覆写,防止方法写错@Deprecated-----> 不建议使用的操作 ,如Thread类 destory(),resume(),stop(),suspend()-----RUMTIME类型@suppressWarnings----->压制警告,如果有一些警告信息,可以使用其压制掉,不出现警告提示。在此类中存在一个value的字符串数组,可以压制多个警告信息。2.自定义annotation 使用的时候需要设置值,,值可以是默认,可以是枚举中设置的,可以为数组---任何一个都继承了Annotation接口3. Rentention和 RentetionPolicy   ---Rentention的取值是RentetionPolicy中的枚举类型RentetionPolicy  的三个范围---- RentetionPolicy  SOURCE 只在源代码中起作用RentetionPolicy   CLASS 只在编译之后Class中起作用RentetionPolicy   RUNTIME在运行的时候起作用4====Annotation与反射如果一个Annotation要想起作用,必须使用RUNTIME范围。一个Annotation要起作用,肯定要依靠反射机制。通过反射可以取得方法上的annotation全部内容反射类中,Filed,Method,Constructor父类中定义了与Annotation的相关方法Annotation ans[] = 字节码.getAnnotations();取得全部           

    如果是内建的,只能取得@Deprecated,证明isAnnotationPresent()5.Target,Documented,InheritedTarget---->因为在任意位置使用Annotation,如,一个Annotation只想在方法上声明,那么就必须设置其作用范围  @Target注释存在ElementType类型变量,存在8种范围--只能在Annotation中出现,,ElementType ANNOTATION_TYPE--只能在构造方法中出现,,ElementType CONSTRUCTOR--本地变量 ,,ElementType LOCAL_VARIABLE--只能在方法上使用, ElementType METHOD--在参数声明上使用,,,ElementType PARAMETER--在包声明上使用,ElementType PACKAGE--在类和接口上使用 ,,ElementType TYPEDocumented---在文档上的注释格式。用Eclipse的javadoc打包后,注释的信息出现在文档当中/Inherited----表示一个Annotation能否被使用其类的子类继续继承下去,如果没有此注释,则Annotation无法继承下去。加上表示此Annotation可以被子类继承。

     

    =============可变参数的特点,

    //只能出现在参数列表的最后//... 位于变量类型和变量名之间,前后有无空格都可以

    package ZHANG.API; public class VariableParameter { public static void main(String[] args) { System.out.println(add(2,3)); System.out.println(add(2,3,44,2)); } public static int add(int x,int ...args){ int sum = x; // for(int i=0;i<args.length;i++){ // sum += args[i]; // } for(int arg:args){ sum +=arg; } return sum; } }

    //自动装箱,拆箱,享元模式flyweight  i.dispaly(int x,int y) 把很多相同的元素变成一个对象,其它常用的作为它的参数

    package ZHANG.API; public class AutoBox { public static void main(String[] args) { Integer iobj = 2 ;//自动装箱 System.out.println(iobj+34);//自动拆箱 String s1 = new String("abc"); String s2 = new String("abc"); Integer i1 = 12; // i1=137 Integer i2 = 12; // i2=137 返回false -128~127 //享元模式flyweight i.dispaly(int x,int y) 把很多相同的元素变成一个对象,其它常用的作为它的参数 System.out.println(i1==i2); Integer i3 = Integer.valueOf(22); Integer i4 = Integer.valueOf(22); System.out.println(i3==i4); } }

    最新回复(0)