您的位置:首页 > 编程语言 > Java开发

Java注解1--JDK自带注解与自定义注解

2018-01-31 13:36 435 查看

(一)概述及JDK自带注解

最近在学习Java注解,看了慕课网的视视频后感觉明白了很多,但我相信一段时间后肯定会忘记,所以做做笔记。

1.何为注解?

  概念:注解(Annotation),也叫元数据。一种代码级别的说明。它是JDK1.5及以后版本引入的一个特性,与类、接口、枚举是在同一个层次。它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些元素进行说明,注释。

         如下所示,我们在编程中经常遇到的@Override等等都是注解。




2.注解的分类

2.1 按照运行机制  (往后会讲)
源码注解:

       注解只在源码中存在,编译成.class文件就不存在了。
编译时注解:

       注解在源码和.class文件中都存在。(例如:JDK的三个注解)

运行时注解:

      在运行阶段还起作用,甚至会影响运行逻辑的注解。
2.2 按照来源分
               来自JDK的注解:上面图片的三个@Override  @Deprecated  @SuppressWarning,接下来会介绍

               来自第三方的注解:比如Spring框架、Mybatis框架的注解等等,以后再说吧

               我们自己定义的注解:往后会详细地讲


3.JDK自带注解

@Override 表示当前方法覆盖了父类的方法

@Deprecation 表示方法已经过时,方法上有横线,使用时会有警告。

@SuppviseWarnings 表示关闭一些警告信息(通知java编译器忽略特定的编译警告)



直接上代码讲解吧,定义一个接口Animal:



然后定义一个Bird类实现该接口,此时提示需要实现Animal接口的方法:



点击Add unimplemented methods后会自定实现接口方法,此时方法上方出现@Override注解,这是一个标识注解,表示当前方法覆盖了父类的方法。



我们可以再创建一个AngryBird类继承Bird,覆盖父类的run()方法:



最后编写一个Test类测试一下,出现警告:run()方法已经过时。为什么呢?因为我在定义Animal接口时,在run()方法上添加了注解@Deprecation,表示方法已经过时,方法上有横线,使用时会有警告。



当然我们忽略警告,直接运行也是可以的,但如果有强迫症的同学呢,可以增加@SuppressWarnings来去掉警告:



好,最后运行结果为:



(二)自定义注解

先上一段代码有个大概的印象,再慢慢讲解(其实代码注释已经讲解地很清楚了):

[java]
view plain
copy

print?

package diyDescription;  
  
import java.lang.annotation.Documented;  
import java.lang.annotation.ElementType;  
import java.lang.annotation.Inherited;  
import java.lang.annotation.Retention;  
import java.lang.annotation.RetentionPolicy;  
import java.lang.annotation.Target;  
  
@Target({ElementType.METHOD,ElementType.TYPE})  
@Retention(RetentionPolicy.RUNTIME)  
@Inherited  
@Documented  
public @interface Description { //使用@interface关键字定义注解  
  
    //成员以无参数无异常方式声明  
    String desc();  
    /* String desc(int a); 
     * String desc() throws Exception; 
     * 都是错误的声明方式 
     */  
      
    String author();  
    // String author() default ""; 合法的声明  
      
    //可以用default为成员指定一个默认值  
    int age() default 18;  
      
    /* 
     * 如果声明:Map map(); 则会报错: 
     * Invalid type Map for the annotation attribute Description.map;  
     * only primitive type, String, Class, annotation, enumeration  
     * are permitted or 1-dimensional arrays thereof 
     *  
     * 只有原始类型和String, Class, annotation, enumeration才可以 
     */  
}  

package diyDescription;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Description { //使用@interface关键字定义注解

//成员以无参数无异常方式声明
String desc();
/* String desc(int a);
* String desc() throws Exception;
* 都是错误的声明方式
*/

String author();
// String author() default ""; 合法的声明

//可以用default为成员指定一个默认值
int age() default 18;

/*
* 如果声明:Map map(); 则会报错:
* Invalid type Map for the annotation attribute Description.map;
* only primitive type, String, Class, annotation, enumeration
* are permitted or 1-dimensional arrays thereof
*
* 只有原始类型和String, Class, annotation, enumeration才可以
*/
}


1.自定义注解的语法要求

首先看上面代码的下面部分:



1.使用@interface关键字定义注解,注意关键字的位置
2.成员以无参数无异常的方式声明,注意区别一般类成员变量的声明
3.可以使用default为成员指定一个默认值,如上所示
4.成员类型是受限的,合法的类型包括原始类型以及String、Class、Annotation、Enumeration (JAVA的基本数据类型有8种:byte(字节)、short(短整型)、int(整数型)、long(长整型)、float(单精度浮点数类型)、double(双精度浮点数类型)、char(字符类型)、boolean(布尔类型)
5.注解类可以没有成员,没有成员的注解称为标识注解,例如JDK注解中的@Override、@Deprecation 
6.如果注解只有一个成员,并且把成员取名为value(),则在使用时可以忽略成员名和赋值号“=” ,例如JDK注解的@SuppviseWarnings ;如果成员名不为value,则使用时需指明成员名和赋值号"=",例子代码如下:

[java]
view plain
copy

print?

package jtzeng;  
import java.lang.annotation.ElementType;  
import java.lang.annotation.Target;  
@Target({ElementType.TYPE})  
public @interface SingleValue1 {  
    String desc();  
}  
  
  
package jtzeng;  
import java.lang.annotation.ElementType;  
import java.lang.annotation.Target;  
@Target({ElementType.METHOD})  
public @interface SingleValue2 {  
    String value();  
}  
  
  
package jtzeng;  
@SingleValue1( desc = "这是TYPE注解" )  //使用时需指明成员名和赋值号"="  
public class Test {  
    @SingleValue2("这是METHOD注解")    //使用时可以忽略成员名和赋值号“=”  
    public void print() {  
        System.out.println();  
    }  
}   

package jtzeng;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@Target({ElementType.TYPE})
public @interface SingleValue1 {
String desc();
}

package jtzeng;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@Target({ElementType.METHOD})
public @interface SingleValue2 {
String value();
}

package jtzeng;
@SingleValue1( desc = "这是TYPE注解" )  //使用时需指明成员名和赋值号"="
public class Test {
@SingleValue2("这是METHOD注解")    //使用时可以忽略成员名和赋值号“=”
public void print() {
System.out.println();
}
}


2.元注解

         何为元注解?就是注解的注解,就是给你自己定义的注解添加注解,你自己定义了一个注解,但你想要你的注解有什么样的功能,此时就需要用元注解对你的注解进行说明了。
        元注解有4个,如下代码的上面部分:



2.1. @Target

即注解的作用域,用于说明注解的使用范围(即注解可以用在什么地方,比如类的注解,方法注解,成员变量注解等等)

取值:
          ElemenetType.CONSTRUCTOR----------------------------构造器声明 

          ElemenetType.FIELD --------------------------------------域声明(包括 enum 实例) 

          ElemenetType.LOCAL_VARIABLE------------------------- 局部变量声明 

          ElemenetType.METHOD ----------------------------------方法声明 

          ElemenetType.PACKAGE --------------------------------- 包声明 

          ElemenetType.PARAMETER ------------------------------参数声明 

          ElemenetType.TYPE--------------------------------------- 类,接口(包括注解类型)或enum声明

使用实例:
        首先定义一个Description注解,

[java]
view plain
copy

print?

package jtzeng;  
import java.lang.annotation.ElementType;  
import java.lang.annotation.Target;  
  
@Target({ElementType.TYPE,ElementType.FIELD})  
public @interface Description {   
  
    String desc();  
    String author();  
    int age() default 21;  
}   

package jtzeng;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target({ElementType.TYPE,ElementType.FIELD})
public @interface Description {

String desc();
String author();
int age() default 21;
}


        然后再定义一个Test类进行测试,为了能让大家看出错误,此处用图片给出。
        可以发现,因为上面定义注解时候,@Target只包含了ElemenetType.TYPEElemenetType.FIELD,所以在类和声明中注解是可以的,而在方法上注解会报错。



2.2. @Retention

描述的注解在什么范围内有效。

取值有:
          RetentionPolicy.SOURCE--------------------------只在源码显示,编译时会丢失

          RetentionPolicy.CLASS-----------------------------编译时会记录到class中,运行时忽略 

          RetentionPolicy.RUNTIME------------------------- 运行时存在,可以通过反射读取

使用实例:
         下面给出的是简单的定义,至于会有什么不同的效果,往后的解析注解部分会讲解。

[java]
view plain
copy

print?

package jtzeng;  
import java.lang.annotation.Retention;  
import java.lang.annotation.RetentionPolicy;  
  
@Retention(RetentionPolicy.RUNTIME)     //运行时存在,可以通过反射读取  
//@Retention(RetentionPolicy.SOURCE)    //只在源码显示,编译时会丢失  
//@Retention(RetentionPolicy.CLASS)     //编译时会记录到class中,运行时忽略   
public @interface Description {   
    String desc();  
    String author() default "JTZeng";  
    int age() default 21;  
}  

package jtzeng;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)     //运行时存在,可以通过反射读取
//@Retention(RetentionPolicy.SOURCE)    //只在源码显示,编译时会丢失
//@Retention(RetentionPolicy.CLASS)     //编译时会记录到class中,运行时忽略
public @interface Description {
String desc();
String author() default "JTZeng";
int age() default 21;
}


2.3. @Inherited

1. 是一个标记注解,没有成员,表示允许子类继承该注解,也就是说如果一个使用了@Inherited修饰的注解被用于一个class时,则这个注解将被该class的子类继承拥有
2. 使用了@Inherited修饰的注解只能被子类所继承,并不可以从它所实现的接口继承

3. 子类继承父类的注解时,并不能从它所重载的方法继承注解
使用实例:

[java]
view plain
copy

print?

package jtzeng;  
import java.lang.annotation.Inherited;  
  
@Inherited  
public @interface Description {   
    String desc();  
    String author() default "JTZeng";  
    int age() default 21;  
}  

package jtzeng;
import java.lang.annotation.Inherited;

@Inherited
public @interface Description {
String desc();
String author() default "JTZeng";
int age() default 21;
}


2.4. @Documented

@Documented是一个标记注解,没有成员。用于描述其它类型的annotation应该被作为被标注的程序成员的公共API,因此可以被例如javadoc此类的工具文档化。(有点抽象,看例子吧)

使用实例:

[java]
view plain
copy

print?

/* 
 * 测试@Documented的功能 
 */  
package jtzeng;  
import java.lang.annotation.Documented;  
import java.lang.annotation.ElementType;  
import java.lang.annotation.Inherited;  
import java.lang.annotation.Retention;  
import java.lang.annotation.RetentionPolicy;  
import java.lang.annotation.Target;  
@Target({ElementType.METHOD,ElementType.TYPE})  
@Retention(RetentionPolicy.RUNTIME)  
@Inherited  
@Documented  
public @interface Description {   
    String desc();  
    String author() default "JTZeng";  
    int age() default 21;  
}  
  
/* 
 * 定义一个Test测试类,类和方法都有注解 
 */  
package jtzeng;  
@Description(desc="这是TYPE注解",author="JTZeng",age=21)  
public class Test {  
    private String field = "自定义注解";  
    @Description(desc="这是METHOD注解",author="JTZeng",age=21)  
    public void print() {  
        System.out.println(field);  
    }  
}  

/*
* 测试@Documented的功能
*/
package jtzeng;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Description {
String desc();
String author() default "JTZeng";
int age() default 21;
}

/*
* 定义一个Test测试类,类和方法都有注解
*/
package jtzeng;
@Description(desc="这是TYPE注解",author="JTZeng",age=21)
public class Test {
private String field = "自定义注解";
@Description(desc="这是METHOD注解",author="JTZeng",age=21)
public void print() {
System.out.println(field);
}
}


        然后,在Eclipse中,右键项目名称,选择Export,选择Java——>javadoc,下一步,完成。看结果,左边是添加了@Documented的效果,右边是没有添加的效果。



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: