Java基础 注解 学习笔记

注:本文主要记录自学习视频 《兄弟连_马剑威_JAVA基础_372_注解》 

1、认识Annotation

2、系统定义的三个Annotation

3、自定义Annotation

4、Retention和RetentionPolicy

5、反射与Annotation

6、@Documented 注解

7、@Target 注解

8、@Inherited 注解


1、认识Annotation

  • 从JDK1.5开始,Java增加了对元数据(即类的组成单元数据)的支持,也就是注解(Annotation),它是代码里做的特殊标记,这些标记可以在编译、类加载,运行时在不改变原有逻辑的情况下,被读取,并执行相应的处理,通过使用Annotation,程序员可以在源文件中嵌入一些补充的信息。代码分析工具,开发工具和部署工具可以通过这些补充信息进行验证或者进行部署。Annotation类似于修饰符一样被使用,可以用于包,类,构造方法,方法,成员变量,参数,局部变量的声明。
  • 注意:Annotation 是一个接口
  • java.lang.Annotation 接口

2、系统定义的三个Annotation

  • JDK1.5后,在系统中提供了三个Annotation,分别是:@Override、@Deprecated、@SuppressWarnings
  • @Override
  • 表示当前的方法定义将覆盖超类中的方法。如果不小心拼写错误,或者方法签名对不上覆盖的方法,编译器就会发出错误提示
  • @Deprecated
  • 表示一个类或者方法已经不再建议继续使用了,标记为已过时
  • @SuppressWarnings
  • 表示关闭不当的编译器警告信息
eg:
public class User {
    private String name ;

    @Deprecated
    public void setName(String name) {
        this.name = name;
    }
}
public class AnnotationDemo {

    @SuppressWarnings("deprecation")
    public static void main(String []args){
        User user=new User();
        user.setName("haha");
    }
}
在IDE中可以看到由于在User中setName方法添加了 @Deprecated标注,所以在Annotation的 main方法中使用时该方法被划了横线,并且报警告(如果不在main方法头添加 @SuppressWarnings(“deprecation”) 标注)。添加上 @SuppressWarnings 标注后该警告会消失

3、自定义Annotation
注解应用需要三个步骤:
  1. 编写注解
  2. 在类上应用注解
  3. 对应用了注解的类进行反射操作的类
自定义Annotation语法:
访问控制权限 @interface Annotation名称{}
eg:
public @interface MyAnnotation{}
  • 在Annotation中定义变量
public @Interface MyAnnotation{
     public String name();
     public String info();
}

定义变量后,在调用此Annotation时必须设置变量值。
@MyAnnotation(name="XB",INFO="WSDR")
public class Demo{
}

  • 通过default指定变量默认值
有了默认值在使用时可以不设值
public @interface MyAnnotation{
     public String name() default "XB";
     public String info() default "WSDR";
}

  • 定义一个变量的数组,接收一组参数
public @interface MyAnnotation{
     public String[] name;
}

使用时指定数组值
@MyAnnation(name={"XB","XH"})
public class Demo{
}
  • 使用枚举限制变量取值范围
public enum Color{
     RED,GREEN,BLUED
}
public @interface MyAnnotation{
     public Color color();
}


4、Retention和RetentionPolicy
  • Annotation要想决定其作用的范围,通过@Retention指定,而Retention指定的范围由RetentionPolicy决定,RetentionPolicy指定了三种范围:

像override Deprecated 都属于Source类型
  • 示例:
@Retention(value=RetentionPolicy.RUNTIME)
public @interface MyAnnation{
     public String name();
}
Retention是用来描述注解的注解,又称为元注解

5、反射与Annotation
  • 一个Annation真正起作用,必须结合反射机制,在反射中提供了以下的操作方法:
java.lang.reflect.AccessibleObject

实例:
Class<?> cls =Class.forName("com.vince.annotation.Test");
Method met=cls.getMethod("setName");//找到toString方法
if(met.isAnnotationPresent(MyAnnotation.class)){
     MyAnnotation my =(MyAnnotation)met.getAnnotation(MyAnnotation.class);
     String name=my.name();
     String info=my.info();
     System.out.println("name="+name);
     System.out.println("info="+info);
}

6、@Documented注解
此注解表示的是文档化,可以在生成doc文档的时候添加注解
@Documented
@Retention(value=RetentionPolicy.RUNTIME)
public @interface MyAnnotation{
     public String name();
     public String info();
}
可以添加一些DOC注释 

7、@Target注解
@Target注解表示的是一个Annotation的使用范围,例如:默认之前定义的MyAnnotation可以在任意位置上使用

8、 Inherited注解
@Inherited表示一个Annotation是否允许被其子类继承下来
eg:
@Inherited
@Target(ElementType.Type)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation{
     public String name();
     public String info();
}
使用时允许被其子类继承















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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值