java用  @interface Annotation{ } 定义一个注解 @Annotation,一个注解是一个类。
@Override,@Deprecated,@SuppressWarnings为常见的3个注解。
注解相当于一种标记,在程序中加上了注解就等于为程序加上了某种标记,以后,
JAVAC编译器,开发工具和其他程序可以用反射来了解你的类以及各种元素上有无任何标记,看你有什么标记,就去干相应的事。

注解@Override用在方法上,当我们想重写一个方法时,在方法上加@Override,当我们方法
的名字出错时,编译器就会报错,如图:


       注解@Deprecated,用来表示某个类的属性或方法已经过时,不想别人再用时,在属性和方法
上用@Deprecated修饰,如图:

注解@SuppressWarnings用来压制程序中出来的警告,比如在没有用泛型或是方法已经过时的时候,
 如图:

注解@Retention可以用来修饰注解,是注解的注解,称为元注解。
Retention注解有一个属性value,是RetentionPolicy类型的,Enum RetentionPolicy是一个枚举类型,
这个枚举决定了Retention注解应该如何去保持,也可理解为Rentention 搭配 RententionPolicy使用。RetentionPolicy有3个值:CLASS  RUNTIME   SOURCE
用@Retention(RetentionPolicy.CLASS)修饰的注解,表示注解的信息被保留在class文件(字节码文件)中当程序编译时,但不会被虚拟机读取在运行的时候;
用@Retention(RetentionPolicy.SOURCE )修饰的注解,表示注解的信息会被编译器抛弃,不会留在class文件中,注解的信息只会留在源文件中;
用@Retention(RetentionPolicy.RUNTIME )修饰的注解,表示注解的信息被保留在class文件(字节码文件)中当程序编译时,会被虚拟机保留在运行时,
所以他们可以用反射的方式读取。RetentionPolicy.RUNTIME 可以让你从JVM中读取Annotation注解的信息,以便在分析程序的时候使用.

package com.self;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface MyTarget
{ }
定义个一注解@MyTarget,用RetentionPolicy.RUNTIME修饰;
package com.self;
import java.lang.reflect.Method;
public class MyTargetTest
{
 @MyTarget
 public void doSomething()
 {
  System.out.println("hello world");
 }
 
 public static void main(String[] args) throws Exception
 {
  Method method = MyTargetTest.class.getMethod("doSomething",null);
  if(method.isAnnotationPresent(MyTarget.class))//如果doSomething方法上存在注解@MyTarget,则为true
  {
   System.out.println(method.getAnnotation(MyTarget.class));
  }
  }
}
上面程序打印:@com.self.MyTarget(),如果RetentionPolicy值不为RUNTIME,则不打印。

@Retention(RetentionPolicy.SOURCE )
public @interface Override

@Retention(RetentionPolicy.SOURCE )
public @interface SuppressWarnings

@Retention(RetentionPolicy.RUNTIME )
public @interface Deprecated
由上可以看出,只有注解@Deprecated在运行时可以被JVM读取到

注解中可以定义属性,看例子:
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation
{
 String hello() default "gege";
  String world();
  int[] array() default { 2, 4, 5, 6 };
  EnumTest.TrafficLamp lamp() ;
  TestAnnotation lannotation() default @TestAnnotation(value = "ddd");
  Class style() default String.class;
}
上面程序中,定义一个注解@MyAnnotation,定义了6个属性,他们的名字为:
hello,world,array,lamp,lannotation,style.
属性hello类型为String,默认值为gege
属性world类型为String,没有默认值
属性array类型为数组,默认值为2,4,5,6
属性lamp类型为一个枚举,没有默认值
属性lannotation类型为注解,默认值为@TestAnnotation,注解里的属性是注解
属性style类型为Class,默认值为String类型的Class类型

看下面例子:定义了一个MyTest类,用注解@MyAnnotation修饰,注解@MyAnnotation定义的属性都赋了值
@MyAnnotation(hello = "beijing", world="shanghai",array={},lamp=TrafficLamp.RED,style=int.class)
public class MyTest
{
 @MyAnnotation(lannotation=@TestAnnotation(value="baby"), world = "shanghai",array={1,2,3},lamp=TrafficLamp.YELLOW)
 @Deprecated
 @SuppressWarnings("")
 public void output()
 {
  System.out.println("output something!");
 }
}
 接着通过反射读取注解的信息:
public class MyReflection
{
 public static void main(String[] args) throws Exception
 {
  MyTest myTest = new MyTest();
    Class<MyTest> c = MyTest.class;
    Method method = c.getMethod("output", new Class[] {});
       //如果MyTest类名上有注解@MyAnnotation修饰,则为true
  if(MyTest.class.isAnnotationPresent(MyAnnotation.class))
  {
   System.out.println("have annotation");
  }
   if (method.isAnnotationPresent(MyAnnotation.class))
   {
   method.invoke(myTest, null); //调用output方法
   //获取方法上注解@MyAnnotation的信息
     MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);
    String hello = myAnnotation.hello();
   String world = myAnnotation.world();
   System.out.println(hello + ", " + world);//打印属性hello和world的值
   System.out.println(myAnnotation.array().length);//打印属性array数组的长度
   System.out.println(myAnnotation.lannotation().value()); //打印属性lannotation的值
   System.out.println(myAnnotation.style());
   }
    //得到output方法上的所有注解,当然是被RetentionPolicy.RUNTIME修饰的
     Annotation[] annotations = method.getAnnotations();
      for (Annotation annotation : annotations)
  {
   System.out.println(annotation.annotationType().getName());
  }
   }
}
上面程序打印:
have annotation
output something!
gege, shanghai
3
baby
class java.lang.String
com.heima.annotation.MyAnnotation
java.lang.Deprecated

如果注解中有一个属性名字叫value,则在应用时可以省略属性名字不写。
可见,@Retention(RetentionPolicy.RUNTIME )注解中,RetentionPolicy.RUNTIME是注解属性值,属性名字是value,
属性的返回类型是RetentionPolicy,如下:
public @interface MyTarget
{
    String value();
}
可以这样用:
  @MyTarget("aaa")
 public void doSomething()
 {
  System.out.println("hello world");
 }
 
注解@Target也是用来修饰注解的元注解,它有一个属性ElementType也是枚举类型,
值为:ANNOTATION_TYPE CONSTRUCTOR  FIELD LOCAL_VARIABLE METHOD PACKAGE PARAMETER TYPE
如@Target(ElementType.METHOD) 修饰的注解表示该注解只能用来修饰在方法上。
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTarget
{
 String value() default "hahaha";
}
如把@MyTarget修饰在类上,则程序报错,如:
@MyTarget
public class MyTargetTest
注解大都用在开发框架中吧,好了有关注解就学习那么多了,谢谢。

Java注释@interface的用法【转】 --好文章 很好理解的更多相关文章

  1. Java注释@interface的用法【转】

    Java用  @interface Annotation{ } 定义一个注解 @Annotation,一个注解是一个类.@Override,@Deprecated,@SuppressWarnings为 ...

  2. Java注释@interface的用法

    转---------- java用  @interface Annotation{ } 定义一个注解 @Annotation,一个注解是一个类.@Override,@Deprecated,@Suppr ...

  3. java中自定义注释@interface的用法

    一.什么是注释     说起注释,得先提一提什么是元数据(metadata).所谓元数据就是数据的数据.也就是说,元数据是描述数据的.就象数据表中的字段一样,每个字段描述了这个字段下的数据的含义.而J ...

  4. [转]自定义注释@interface的用法

    一.什么是注释     说起注释,得先提一提什么是元数据(metadata).所谓元数据就是数据的数据.也就是说,元数据是描述数据的.就象数据表中的字段一样,每个字段描述了这个字段下的数据的含义.而J ...

  5. Java annotation 自定义注释@interface的用法

    最近看到很多项目都是用了自定义注解,例如 1.什么是注解? 元数据(metadata),就是指数据的数据,元数据是描述数据的,就像数据库中的,表的字段,每一个 字段描述这个字段下面·的数据的含义,j2 ...

  6. Java 自定义注释@interface的用法

    最简单的待校验的注解定义 @Documented @Constraint(validatedBy = ExistBlankByListValidator.class) @Target({PARAMET ...

  7. JAVA注释的另一种神奇用法

    每个JAVA程序员在写程序的时候一定都会用到注释,本篇博客不是讲怎么定义注释,而是说明注释神奇的一种写法. /** * 这是一个测试类 */ public class Test { /** * 程序的 ...

  8. java注释指导手册

    译文出处: Toien Liu   原文出处:Dani Buiza 编者的话:注解是java的一个主要特性且每个java开发者都应该知道如何使用它. 我们已经在Java Code Geeks提供了丰富 ...

  9. Java泛型总结---基本用法,类型限定,通配符,类型擦除

    一.基本概念和用法 在Java语言处于还没有出现泛型的版本时,只能通过Object是所有类型的父类和类型强制转换两个特点的配合来实现类型泛化.例如在哈希表的存取中,JDK1.5之前使用HashMap的 ...

随机推荐

  1. 预分配内存fifo实现可变长度字节序列存储

    预分配内存fifo实现可变长度字节序列存储 github链接https://github.com/gexin1023/utils/tree/master/fifo fifo即先进先出队列,可以用链表来 ...

  2. 二 Capacity Scheduler 计算能力调度器

    官网的写的太难懂,参考:http://www.360doc.com/content/14/0603/14/14935022_383254798.shtml Capacity Scheduler 一种可 ...

  3. 【转载】java byte转十六进制

    public static String bytes2HexString(byte[] b) { String ret = ""; for (int i = 0; i < b ...

  4. php5.4以上运行yii框架出现问题的解决方法

    Ubuntu Server 下安装 Mcrypt Php Extension http://blog.archean.me/2013/10/22/install-mcrypt-php-extensio ...

  5. 算法与数据结构实验题 4.1 伊姐姐数字 game

    ★实验任务 伊姐姐热衷于各类数字游戏,24 点.2048.数独等轻轻松松毫无压力.一 日,可爱的小姐姐邀请伊姐姐一起玩一种简单的数字 game,游戏规则如下: 一开始桌上放着 n 张数字卡片,从左到右 ...

  6. Android中Parcelabel对象的使用和理解

    1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. ...

  7. 实验吧编程题:Hashkill

    原题:6ac66ed89ef9654cf25eb88c21f4ecd0是flag的MD5码,(格式为ctf{XXX_XXXXXXXXXXX_XXXXX})由一个0-1000的数字,下划线,纽约的一个区 ...

  8. Android------去除标题栏

    这里暂时只给出一种方法,在java代码中去除 1.继承Activity 在onCreate方法中 getWindow().setFlags(WindowManager.LayoutParams.FLA ...

  9. 如何设计好的RESTful API之安全性

    保证RESTful API的安全性,主要包括三大方面: a) 对客户端做身份认证 b) 对敏感的数据做加密,并且防止篡改 c) 身份认证之后的授权 1.对客户端做身份认证,有几种常见的做法: 1)在请 ...

  10. 【BZOJ4520】K远点对(KD-Tree)

    [BZOJ4520]K远点对(KD-Tree) 题面 BZOJ 洛谷 题解 考虑暴力. 维护一个大小为\(K\)的小根堆,然后每次把两个点之间的距离插进去,然后弹出堆顶 这样子可以用\(KD-Tree ...