詳解Java進階知識注解
注解
叫元數(shù)據(jù),一種代碼級別的說明,它是JDK1.5及以后版本引入的一個特性,與類、接口、枚舉在同一個層次,它可以聲明在包、類、字段、局部變量、方法參數(shù)等的前面,用來對這些元素進行說明、注釋。
注解的作用分類
編寫文檔:通過代碼里表示的元數(shù)據(jù)生成文檔【生成doc文檔】 代碼分析:通過代碼里表示的元數(shù)據(jù)進行分析【使用反射】 編譯檢查:通過代碼里表示的元數(shù)據(jù)讓編譯器能夠實現(xiàn)基本的編譯檢查【Override】注解按照運行機制分類
源碼注解:注解只在源碼中存在,編譯成.class文件之后就不存在了 編譯時注解:注解在源碼存在的基礎上,也會在.class文件中存在,但是在運行階段中就不存在了,例如:@Override 運行時注解:注解在運行階段依然存在,且能夠影響程序的運行過程,例如:@Autowired2、注解與注釋的區(qū)別(1)注解:用于描述代碼,說明程序,主要目的是為了給計算機看,且能夠影響程序的運行。
(2)注釋:用于描述代碼的作用和一些關鍵性的知識點,使用文字描述程序,是為了給程序員觀看,以此來使程序員能夠以最快的時間了解被注釋的代碼。
二、內置注解與元注解1、常用的內置注解 @Override:檢測該注解標記的方法是否繼承自父類; @Deprecated:說明被標記的內容已過時,暗示著在不久之后可能會被更新抹除; @SuppressWarnings:壓制警告,就是被標記的部分不會產生警告,常用的參數(shù):@SuppressWarnings(“all”); @SafeVarargs:參數(shù)安全類型注解,它的目的就是提醒開發(fā)者不要用參數(shù)做一些不安全的操作,它的存在會阻止編譯器產生unchecked這樣的警告;2、常用的元注解元注解:用于描述注解的注解,在創(chuàng)建注解時使用
1. @Target屬性值:
ElementType.TYPE:能修飾類、接口或枚舉類型 ElementType.METHOD:能修飾方法 ElementType.FIELD: 能修飾成員變量 ElementType.PARAMETER:能修飾參數(shù) ElementType.CONSTRUCTOR:能夠修飾構造器 ElementType.ANNOTATION_TYPE:能夠修飾注解 ElementType.PACKAGE:能夠修飾包 ElementType.LOCAL_VARIABLE:能夠修飾局部變量2.@Retention屬性值:
RetentionPolicy.SOURCE:注解只在源碼中存在,編譯成class之后就沒了 RetentionPolicy.CLASS:注解在源碼和class中都存在,運行時就沒了,這個是Retention的默認值 RetentionPolicy.RUNTIME: 注解在源碼、class、運行時都存在,如果要使用反射一定要定義為這種類型3.@Documented:該注解的作用就是表示此注解標記的注解可以包含到javadoc文件中去4.@Inherited:描述注解是否能夠被子類所繼承
三、自定義注解1、自定義注解基礎知識1.格式:
@Inherited//元注解public @interface zhujie{}
2.注解本質:注解的本質上就是一個接口,該接口默認繼承Annotation
public interface MyAnno extends java.lang.annotation.Annotion
3.屬性:接口中可以定義的內容(成員方法、抽象方法)
屬性的返回值:
八種基本數(shù)據(jù)類型 字符串類、接口、枚舉 注解 以上類型的數(shù)組屬性賦值注意事項
如果定義屬性時,使用default關鍵字給屬性默認初始化值,則使用注解時,就可以不進行屬性的賦值,否則都必須給屬性賦值 如果只有一個屬性需要賦值的話,并且屬性的名稱是value,則使用注解給屬性賦值時,value可以省略,直接定義值就可以了 數(shù)組賦值時,值需要使用{}包裹,如果數(shù)組中只有一個值,則{}可以省略不寫2、演示自定義注解的使用自定義注解annotation
@Retention(value = RetentionPolicy.RUNTIME)@Target(value = ElementType.TYPE)public @interface annotation { String name() default '木魚'; int age(); int[] score();}
使用以上注解的類TestAnnotation
//name具有默認值,不需要必須為name賦值,但也可以重新賦值@annotation(age=20,score={99,100,100})public class TestAnnotation { public static void main(String[] args) throws ClassNotFoundException {Class clazz = Class.forName('test.TestAnnotation');annotation annotation = (annotation) clazz.getAnnotation(annotation.class);System.out.println('姓名:'+annotation.name()+' 年齡:'+annotation.age());System.out.print('成績?yōu)椋?);int[] score=annotation.score();for (int score1:score){ System.out.print(score1+' ');} }}
運行結果
兩個方法:
類對象調用 isAnnotationPresent(Class<? extends Annotation> annotationClass)判斷是否應用了某個注解 通過getAnnotation()方法獲取Annotation對象,或者getAnnotations()方法獲取所有應用在該類上的注解1.創(chuàng)建自定義注解
@Retention(value = RetentionPolicy.RUNTIME)@Target(value = ElementType.FIELD)public @interface StringNull {}
2.創(chuàng)建實體類
public class Student { @StringNull public String name=null; @StringNull public String xuehao=null; @StringNull public String sex=null; public void setName(String name) {this.name = name; } public void setXuehao(String xuehao) {this.xuehao = xuehao; } public void setSex(String sex) {this.sex = sex; }}
3.創(chuàng)建測試類,測試注解
public class TestAnnotation { public static void main(String[] args) throws Exception{Class clazz = Class.forName('test.Student');Student student =(Student) clazz.newInstance();student.setName('小明');Field[] fields= clazz.getFields();for(Field f:fields){ if(f.isAnnotationPresent(StringNull.class)){if(f.get(student)==null){ System.out.println(f.getName()+':是空的字符串屬性');}else{ System.out.println(f.getName()+':'+f.get(student));} }} }}
4.運行結果
以上就是詳解Java進階知識注解的詳細內容,更多關于Java注解的資料請關注好吧啦網其它相關文章!
相關文章:
