注解的讲解

什么是注解

Annotation的作用
1.不是程序本身 , 可以对程序作出解释.(这一点和注释(comment)没什么区别)
2.可以被其他程序(比如:编译器等)读取.

Annotation的格式 :
1.注解是以"@注释名"在代码中存在的 , 还可以添加一些参数值
2.例如:@SuppressWarnings(value=“unchecked”).

Annotation在哪里使用?
可以附加在package , class , method , field 等上面 , 相当于给他们添加了额外的辅助信息, 我们可以通过反射机制编程实现对这些元数据的访问。

内置注解

1.@Override:定义在java.lang.Override中,此注释只适用于修辞方法,表示一个方法声明打算重写超类中的另一个方法声明
2.@Deprecated:定义在java.lang.Deprecated,此注释可以用于修辞方法,属性,类,表示不鼓励程序员使用这样的元素,通常是因为它很危险或者存在更好的选择。
3.@SuppressWarnings:定义在java.lang.SuppressWarnings中,用来抑制编译时的警告信息。与前两个注释有所不同,你需要添加一个参数才能正确使用,这些参数都是已经定义好了的,我们选择性的使用就好了
@SuppressWarnings(“all”)
@SuppressWarnings(“unchecked”)
@SuppressWarnings(value = {“unchecked”,deprecation})
等等…

元注解

  • 元注解的作用就是负责注解其他注解,Java定义了4个标准的meta-annotation类型,他们被用来提供其他annotation类型说明.
  • 这些类型和它们所支持的类在java.lang.annotation包中可以找到.(@Target,@Retention,@Documented,@Inherited)
    @Target:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)
    @Retention:表示需要在什么级别保存该注解信息,用于描述注解的生命周期(SOURCE < CLASS < RUNTIME)
    @Document:说明该注解即将包含在javadoc中
    @Inherited:说明子类可以继承父类中的该注解

*### 自定义注解
使用@interface 自定义注解时,自动继承了java.lang.annotation.Annotation接口

分析
@interface 用来声明一个注释,格式:public @interface 注解名{定义内容}
其中的每一个方法实际上是声明了一个配置参数
方法的名称就是参数的名称
返回值类型就是参数的类型(返回值只能是基本类型,Class,String ,enum).
可以通过default来声明参数的默认值
如果只有一个参数成员,一般参数名为value
注解元素必须要有值,我们定义注解元素时,经常使用空字符串,0作为默认值.

下面就以代码为例看一看什么是注解:

public class AnnotationTest1 {
    public static void main(String[] args) {
        int year = new Date().getYear();
        /**
         * 该方法的内置注解是不建议你使用这个方法
         * 源码:
         * @Deprecated
         * public int getYear() {
         * eturn normalize().getYear() - 1900;
         * }
         */

    }
    //不建议使用该方法
    @Deprecated
    public int Test() {
        System.out.println("123");
        return 1;
    }
    //@Override 内置注解 只能定义在方法上  并且是重写了顶层父类的方法
    @Override
    public String toString() {
        return "AnnotationTest1{}";
    }
public class AnnotationTest2  {
    public static void main(String[] args) {

    }
        /**
         * 如何定义个注解
         */
        //Annotation 语法:
        // public @interface 注解名{定义内容}
    private String name;
    @MyAnnotation
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


@Target(ElementType.METHOD) //表示这个注解,可以注解 方法、属性(字段)、类
@Retention(RetentionPolicy.RUNTIME)    //通常使用的是RetentionPolicy.RUNTIME 表示在运行生效;
@Documented //表示可以在Javadoc中生成信息,没有什么作用;
@Inherited //表示的是子类可以继承父类的注解,一般也不用;
@interface MyAnnotation {
}
public class AnnotationTest3 {

        private int age;
        private String name;

        public AnnotationTest3() {
        }

        public AnnotationTest3(int age, String name) {
            this.age = age;
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;

        }

        public String getName() {
            return name;
        }

        @MyAnnotation2(schools = "123")
        public void setName(String name) {
            this.name = name;
        }

}

@Target(value = {ElementType.METHOD})
@Retention(value = RetentionPolicy.RUNTIME)
@interface MyAnnotation3 {
    String[] value();//只有一个参数 value 可以省略
}



@Target(value = {ElementType.METHOD})//注解在方法上
@Retention(value = RetentionPolicy.RUNTIME)//注解运行时生效
@interface MyAnnotation2 {
    String name() default "";
    int age() default 0;//通过default定义默认值;
    int id() default -1;//-1表示找不到
    String[] schools();
}

看完这些代码我和大家一样会有一些问题?

  • 注解的作用是什么?
  • 可以干嘛?
  • 定义的内容起什么作用
  • 我们可以通过反射机制编程实现对这些元数据访问

下一节通过反射机制来探讨一些注解的作用到底是什么

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值