java中的注解

jdk1.5开始就增加了对元数据的支持,也就是注解(Annotation),这种标记可以在编译,类加载,运行的时候被读取。

1 有几种基本的注解

 1) @Override(重写父类方法):只能用于方法,不能用于其他程序元素。用来指定方法覆盖。常常可以用来检查是否覆盖父类的方法

 public class Erica {

 @Override
 public boolean equals(Object obj) {
  // TODO Auto-generated method stub
  return super.equals(obj);
 }

}

 

把以上的方法改为,则会报错,因为父类没有如下方法。

@Override
 public boolean equals(Erica obj) {
  // TODO Auto-generated method stub
  return super.equals(obj);
 }

2)  @Deprecated(标记已经过时):表示某个程序元素已过时,当其他程序使用已过时的类,方法时,编译器会发出警告.

 

 

3 @SuppressWarnings(抑制编译警告):作用于该程序元素中的所有子元素,取消显示的编译警告.平时用的很少。

 

二:修饰注释的元注解

1 定义一个注解,一般定义一个新的注解使用@interface关键字,与定义一个接口很像。

public @interface  EricaAnnotation {
  
}

 

2编写一个类,使用其上的注解,并用反射打印出其使用的注解,看下图会发现其命令行啥也没有打印出来,明明用到了,注解却啥也没有打印出来,这是为什么呢?

 

 

这是因为可以用@Retention可以指定被修饰的注解保留多久

@RetentionPolicy.SOURCE:只保留在源码中

@RetentionPolicy.CLASS:编译器把注解保留在class中,运行时不在保留注解

@RetentionPolicy.RUNNTIME:class中有注解,运行时也有注解,程序可以通过反射得到注解信息。

下面是对应关系:

source         -        class                                                               -runtime

JAVA源文件-CLASS文件(jvm编译成class,class文件不是字节码)-内存中的字节码

 

把以上注解修改为如下,再次运行命令行就可以打印出注解信息了:

@Retention(RetentionPolicy.RUNTIME) 

  public @interface  EricaAnnotation {
  
}

也可以使用@Target修饰注解只能使用于哪些元素:

如下:

@Retention(RetentionPolicy.RUNTIME) 
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface  EricaAnnotation {
  
}

上面代码定义该注解只能使用于方法和类,接口或者枚举定义。

 

三:为注解增加属性

1 为注解增加基本属性

@Retention(RetentionPolicy.RUNTIME) 
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface  EricaAnnotation {
   String color() default "blue";
  
}

使用注解的时候定义属性值

@EricaAnnotation(color="red")
public class AnnotationTest {

 

}

 

2 为注解增加数组属性

@Retention(RetentionPolicy.RUNTIME) 
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface  EricaAnnotation {
 
   String color() default "blue";
   int[]arrayAttr();
}

使用注解时的定义数组属性值:

@EricaAnnotation(color="red",arrayAttr={1,2,3})
public class AnnotationTest {

}

 

3 为注解新增枚举属性:

  定义枚举:

public enum Season {

 spring ,summer,autumn,winter
}

 

在注解上面使用枚举属性

public @interface  EricaAnnotation {
 
   String color() default "blue";
   int[]arrayAttr();
   Season  season();//枚举
  
}

为注解的枚举属性赋值

@EricaAnnotation(color="red",arrayAttr={1,2,3},season=Season.summer)
public class AnnotationTest {

 

}

 

4 新增注解类型的属性

 定义一个新的注解

public @interface MetaAnnotation {
 String value();
}

新增一个注解类型的属性

@Retention(RetentionPolicy.RUNTIME) 
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface  EricaAnnotation {
 
   String color() default "blue";
   int[]arrayAttr();
   Season  season();
   MetaAnnotation  annotationAttr();//注解

 

为注解的枚举属性赋值

@EricaAnnotation(color="red",arrayAttr={1,2,3},season=Season.summer,annotationAttr=@MetaAnnotation("wl"))
public class AnnotationTest {

 

}

 

最后是这些新增的注解,运用反射打印出来的值:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值