2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
java基礎(chǔ)鞏固筆記(6)-注解 標(biāo)簽: java
[TOC]
注解(Annotation),也叫元數(shù)據(jù)。一種代碼級(jí)別的說(shuō)明。它是JDK1.5及以后版本引入的一個(gè)特性,與類、接口、枚舉是在同一個(gè)層次。它可以聲明在包、類、字段、方法、局部變量、方法參數(shù)等的前面,用來(lái)對(duì)這些元素進(jìn)行說(shuō)明,注釋。
API
Package java.lang.annotation
注解的應(yīng)用結(jié)構(gòu)圖 調(diào)用/結(jié)構(gòu)關(guān)系:A<--B<--C
A,B,C解釋如下:
A:注解類
@interface A{
}
B:應(yīng)用了“注解類”的類
@A
Class B{
}
C:對(duì)“應(yīng)用了注解類的類”進(jìn)行反射操作的類
Class C{public void f(){B.class.isAnnotationPresent(A.class);A a = B.class.getAnnotion(A.class);}
}
元注解 元注解的作用就是負(fù)責(zé)注解其他注解。四個(gè)元注解分別是:@Target,@Retention,@Documented,@Inherited
表示在什么級(jí)別保存該注解信息。可選的參數(shù)值在枚舉類型 RetentionPolicy中,包括RetentionPolicy.SOURCE,RetentionPolicy.CLASS(默認(rèn)),RetentionPolicy.RUNTIME分別對(duì)應(yīng):java源文件-->class文件-->內(nèi)存中的字節(jié)碼
RetentionPolicy.SOURCE 注解將被編譯器丟棄
RetentionPolicy.CLASS 注解在class文件中可用,但會(huì)被VM丟棄
RetentionPolicy.RUNTIME VM將在運(yùn)行期也保留注釋,因此可以通過(guò)反射機(jī)制讀取注解的信息。
表示該注解用于什么地方,可能的值在枚舉類ElemenetType中,包括
ElemenetType.CONSTRUCTOR 構(gòu)造器聲明
ElemenetType.FIELD 域聲明(包括 enum 實(shí)例)
ElemenetType.LOCAL_VARIABLE 局部變量聲明
ElemenetType.METHOD 方法聲明
ElemenetType.PACKAGE 包聲明
ElemenetType.PARAMETER 參數(shù)聲明
ElemenetType.TYPE 類,接口(包括注解類型)或enum聲明
將此注解包含在javadoc中 ,它代表著此注解會(huì)被javadoc工具提取成文檔。在doc文檔中的內(nèi)容會(huì)因?yàn)榇俗⒔獾男畔?nèi)容不同而不同。相當(dāng)于@see,@param等
允許子類繼承父類中的注解
自定義注解 使用@interface自定義注解時(shí),自動(dòng)繼承了java.lang.annotation.Annotation接口,由編譯程序自動(dòng)完成其他細(xì)節(jié)。在定義注解時(shí),不能繼承其他的注解或接口。@interface用來(lái)聲明一個(gè)注解,其中的每一個(gè)方法實(shí)際上是聲明了一個(gè)配置參數(shù)。方法的名稱就是參數(shù)的名稱,返回值類型就是參數(shù)的類型(返回值類型只能是基本類型、Class、String、enum)。可以通過(guò)default來(lái)聲明參數(shù)的默認(rèn)值。
定義注解格式:
public @interface 注解名 {定義體}
注解參數(shù)的可支持?jǐn)?shù)據(jù)類型:
1.所有基本數(shù)據(jù)類型(int,float,boolean,byte,double,char,long,short) 2.String類型 3.Class類型 4.enum類型 5.Annotation類型 6.以上所有類型的數(shù)組
示例代碼 參考文末的【參考資料】中《java 注解的幾大作用及使用方法詳解(完)》
下面的示例,是上文提到的A<--B<--C 的擴(kuò)充版本。自定義了一個(gè)注解@A,然后在B類中使用了注解@A,最后在類C中利用反射讀取@A中的信息
package com.iot.annotation;import java.lang.annotation.*;/*** Created by brian on 2016/2/20.*/
@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD,ElementType.CONSTRUCTOR})
@Retention(RetentionPolicy.RUNTIME)
public @interface A {String name();int id() default 0;Class<Long> gid();
}
package com.iot.annotation;import java.util.HashMap;
import java.util.Map;/*** Created by brian on 2016/2/20.*/
@A(name="type",gid=Long.class)//類注解
public class B {@A(name="param",id=1,gid=Long.class) //類成員注解private Integer age;@A(name="construct",id=2,gid=Long.class) //構(gòu)造方法注解public B(){}@A(name="public method",id=3,gid=Long.class) //類方法注解public void a(){}@A(name="protected method",id=4,gid=Long.class) //類方法注解protected void b(){Map<String,String> m = new HashMap<String,String>(0);}@A(name="private method",id=5,gid=Long.class) //類方法注解private void c(){Map<String,String> m = new HashMap<String,String>(0);}public void b(Integer a){}
}
package com.iot.annotation;import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;/*** Created by brian on 2016/2/20.*/
public class C {/*** 簡(jiǎn)單打印出B類中所使用到的類注解* 該方法只打印了 Type 類型的注解* @throws ClassNotFoundException*/public static void parseTypeAnnotation() throws ClassNotFoundException{Class clazz = Class.forName("com.iot.annotation.B");Annotation[] annotations = clazz.getAnnotations();for(Annotation annotation :annotations){A a = (A)annotation;System.out.println("id = "+a.id()+" ;name = "+a.name()+" ;gid = "+a.gid());}}/*** 簡(jiǎn)單打印出B類中所使用到的方法注解* 該方法只打印了 Method 類型的注解*/public static void parseMethodAnnotation() {Method[] methods = B.class.getDeclaredMethods();for (Method method : methods) {/** 判斷方法中是否有指定注解類型的注解*/boolean hasAnnotation = method.isAnnotationPresent(A.class);if (hasAnnotation) {/** 根據(jù)注解類型返回方法的指定類型注解*/A annotation = method.getAnnotation(A.class);System.out.println("method = " + method.getName()+ " ; id = " + annotation.id() + " ; description = "+ annotation.name() + "; gid= " + annotation.gid());}}}/*** 簡(jiǎn)單打印出B類中所使用到的方法注解* 該方法只打印了 Method 類型的注解*/public static void parseConstructAnnotation(){Constructor[] constructors = B.class.getConstructors();for (Constructor constructor : constructors) {/** 判斷構(gòu)造方法中是否有指定注解類型的注解*/boolean hasAnnotation = constructor.isAnnotationPresent(A.class);if (hasAnnotation) {/** 根據(jù)注解類型返回方法的指定類型注解*/A annotation =(A) constructor.getAnnotation(A.class);System.out.println("constructor = " + constructor.getName()+ " ; id = " + annotation.id() + " ; description = "+ annotation.name() + "; gid= "+annotation.gid());}}}public static void main(String[] args) throws ClassNotFoundException {parseTypeAnnotation();parseMethodAnnotation();parseConstructAnnotation();}}
參考資料 java 注解的幾大作用及使用方法詳解(完) 另類的package-info.java文件探討 深入理解Java:注解(Annotation)自定義注解入門 作者@brianway更多文章:個(gè)人網(wǎng)站 | CSDN | oschina
轉(zhuǎn)載于:https://my.oschina.net/brianway/blog/618037
創(chuàng)作挑戰(zhàn)賽 新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)
總結(jié)
以上是生活随笔 為你收集整理的java基础巩固笔记(6)-注解 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。