黑马程序员_注解

来源:互联网 发布:js 获取对象的属性值 编辑:程序博客网 时间:2024/05/17 03:19
---------- android培训、java培训、java学习型技术博客,期待与您交流!----------
        注解是插入到源代码中用于某种工具处理的标记。在程序中加了注解后,编译器、开发工具和其他程序就可以通过反射来了解类及类的各种元素上有无何种标记,根据标记做相应的处理。注解可以加在包、类、字段、方法、方法的参数以及局部变量上。
        常见的注解是这三个:@Override ,@Deprecated   和@SuppressWarnings   。其中@Override 用在方法上,用于检查该方法是否覆盖了某个父类方法,@Deprecated   用于所有元素,将项标记为已过时,@SuppressWarnings   用于除包和注解之外的所有情况,作用是取消显示指定的编译器警告。
        由前文不难推知注解还有标记在注解上的情况,该类注解称为元注解。元注解包括:@Retention、@Target、@Documented  和@Inheried  。这四个元注解的功能分别对应:指定该注解可以保留多久;指定可以应用该注解的那些项;指定该注解应该包含在注解项的文档中;指定一个允许子类继承的父类中的注解。
        元注解@Retention    的RetentionPolicy 共有三种策略:
SOURCE: 这个Annotation类型的信息只会保存在程序源码中,源码如果经过了编译之后,Annotation的数据就会消失,并不会保存在编译好的.class二进制文件中。CLASS: 这个Annotation类型的信息保留在程序源码中,同时也会保存在编译好的.class文件中,在执行的时候并不会加载到JVM中。(默认)RUNTIME: 表示在源码、编译好的.class文件中保存信息,在执行的时候会把这些信息加载到JVM中,这样可以使用反射将其信息获取出来。

        元注解@Target注解的元素类型有这么一些:
ANNOTATION_TYPE: 注释类型CONSTRUCTOR: 构造方法FIELD: 字段(包括枚举常量)LOCAL_VARIABLE: 局部变量METHOD: 方法PACKAGE: 包PARAMETER: 参数TYPE: 类Class、接口Interface(包括注释类型Annotation)或枚举Enum

        我们可以自定义注解,来看示例代码:
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,ElementType.TYPE})public @interface ItcasAnnotation {}

import javax.jws.soap.InitParam;@ItcastAnnotationpublic class AnnotationTest {@SuppressWarnings("deprecation")public static void main(String[] args) throws Exception {System.runFinalizersOnExit(true);//判断ItcastAnnotation.class这个注解是否存在if(AnnotationTest.class.isAnnotationPresent(ItcastAnnotation.class)) {ItcastAnnotation annotation = (ItcastAnnotation)AnnotationTest.class.getAnnotation(ItcastAnnotation.class);System.out.println(annotation);}}@Deprecatedpublic static void sayHello() {System.out.println("hi,itcast");}}

        在上面的代码中,ElementType.METHOD 和 ElementType.TYPE 表明了自定义注解只能标注在方法、类(包括枚举)及接口上。
        注解中还可以添加属性。再来看代码:
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,ElementType.TYPE})public @interface ItcasAnnotation {String color() default "blue";String value();int[] arrayAttr() default {3,4,4};//添加一个枚举EnumTest.TrafficLamp lamp() default EnumTest.TrafficLamp.RED;//注解类型的方法,指定了默认值MetaAnnotation annotationAttr() default @MetaAnnotation("lhm");}

public @interface MetaAnnotation {String value();}

import javax.jws.soap.InitParam;@ItcastAnnotation(annotationAttr=@MetaAnnotation("flx"),color="red",value="abc",arrayAttr={1,2,3})public class AnnotationTest {@SuppressWarnings("deprecation")@ItcastAnnotation("xyz")public static void main(String[] args) throws Exception {System.runFinalizersOnExit(true);//判断ItcastAnnotation.class这个注解是否存在if(AnnotationTest.class.isAnnotationPresent(ItcastAnnotation.class)) {ItcastAnnotation annotation = (ItcastAnnotation)AnnotationTest.class.getAnnotation(ItcastAnnotation.class);System.out.println(annotation.color());System.out.println(annotation.value());System.out.println(annotation.arrayAttr().length);System.out.println(annotation.lamp().nextLamp().name());System.out.println(annotation.annotationAttr().value());}}@Deprecatedpublic static void sayHello() {System.out.println("hi,itcast");}}

        在上面的示例代码中,出现了两个自定义注解:ItcastAnnotatio 和 MetaAnnotation。default为属性指定了缺省值,如果没有指定则必须在注解使用时确定属性。此外还使用到了一个Lamp.java的枚举,不过该枚举就不再贴出代码了。

---------- android培训、java培训、java学习型技术博客,期待与您交流!----------
原创粉丝点击