- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- java高級用法之注解和反射講義
反射和注解在java中偏高級用法,一般在各種框架中被廣泛應用,文章簡(jiǎn)單介紹下反射和注解的用法,希望對你的工作學(xué)習有一定幫助
什么是注解
Java 注解也就是Annotation是從 Java5 開(kāi)始引入的新技術(shù)
Annotation的作用:
Annotation的格式:
Annotation在里使用?
元注解
元注解的作用就是負責注解其他注解,java定義了4個(gè)標準的meta-annotation類(lèi)型,被用來(lái)提供對其他annotation類(lèi)型作說(shuō)明
這些類(lèi)型和它們所支持的類(lèi)在java.lang.annotation包中可以找到(@Target,@Retention,@Documented,@Inherited)
自定義注解
使用@interface自定義注解時(shí),自動(dòng)繼承了java.lang.annotation.Annotation接口
public class Test03 { //注解可以顯示賦值,如果沒(méi)有默認值,一定要給注解賦值 @Myannotation2(name = "aj",schloos = {"機電學(xué)院"}) public void test(){ } @MyAnnotation3("") public void test2(){ } } @Target({ElementType.METHOD,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @interface Myannotation2{ // 注解的參數,參數類(lèi)型+參數名 String name() default ""; int age() default 0; //如果默認值為-1 代表不存在 int id() default -1; String[] schloos() ; } @Target({ElementType.METHOD,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation3{ String value(); }
給代碼加注解其實(shí)就是這么多,關(guān)鍵還是我們如何去讀取注解,這就需要用到反射,下面重點(diǎn)介紹java反射
反射是java被視為動(dòng)態(tài)語(yǔ)言的關(guān)鍵,反射機制允許程序在執行期借助Reflection API取得任何類(lèi)的內部信息,并能直接操作任意對象內部熟悉及方法
Class c = Class.forName("java.lang.String")
加載完類(lèi)之后,在堆內存的方法區就產(chǎn)生了一個(gè)Class類(lèi)型的對象(一個(gè)類(lèi)只有一個(gè)Class對象),這個(gè)對象就包含了完整的類(lèi)的結構信息。我們可以通過(guò)這個(gè)對象看到類(lèi)的結構。這個(gè)對象就像一面鏡子,透過(guò)這個(gè)鏡子看到類(lèi)的結構,所以我們稱(chēng)之為:反射
Class類(lèi)
對于每個(gè)類(lèi)而言,JRE都為其保留一個(gè)不變的Class類(lèi)型的對象,一個(gè)Class對象包含了特定某個(gè)結構的有關(guān)信息。
Class類(lèi)的常用方法
反射獲取對象
public class Test02 { public static void main(String[] args) throws ClassNotFoundException { Person person = new Student(); System.out.println("這個(gè)人是"+person.name); //通過(guò)對象獲取 Class c1 = person.getClass(); System.out.println(c1.hashCode()); //通過(guò)forname獲取 Class c2 = Class.forName("reflection.Student"); System.out.println(c2.hashCode()); //通過(guò)類(lèi)名獲取 Class c3 = Student.class; System.out.println(c3.hashCode()); //獲得父類(lèi)類(lèi)型 Class c4 = c1.getSuperclass(); System.out.println(c4); } } @Data class Person{ public String name; public int age; } class Student extends Person{ public Student(){ this.name = "學(xué)生"; } } class Teacher extends Person{ public Teacher(){ this.name = "老師"; } }
反射操作方法、屬性
public class Test03 { public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException { Class c1 = Class.forName("reflection.Student"); Student student = (Student) c1.newInstance(); System.out.println(student.getName()); // 通過(guò)反射操作方法 Method setName = c1.getDeclaredMethod("setName", String.class); setName.invoke(student, "zhangshan"); System.out.println(student.getName()); Student student1 = (Student) c1.newInstance(); Field name = c1.getDeclaredField("name"); //反射不能直接操作私有屬性,需要手動(dòng)關(guān)掉程序的安全檢測,setAccessible(true) name.setAccessible(true); name.set(student1,"lisi"); System.out.println(student1.getName()); } }
性能檢測
public class Test04 { public static void test01(){ User user = new User(); long startTime = System.currentTimeMillis(); for (int i = 0; i <1000000000 ; i++) { user.getName(); } long endTime = System.currentTimeMillis(); System.out.println(endTime - startTime +"ms"); } public static void test02() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { User user = new User(); long startTime = System.currentTimeMillis(); Class c1 = user.getClass(); Method getName = c1.getDeclaredMethod("getName", null); for (int i = 0; i <1000000000 ; i++) { getName.invoke(user, null); } long endTime = System.currentTimeMillis(); System.out.println(endTime - startTime +"ms"); } public static void test03() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { User user = new User(); long startTime = System.currentTimeMillis(); Class c1 = user.getClass(); Method getName = c1.getDeclaredMethod("getName", null); getName.setAccessible(true); for (int i = 0; i <1000000000 ; i++) { getName.invoke(user, null); } long endTime = System.currentTimeMillis(); System.out.println(endTime - startTime +"ms"); } public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { test01(); test02(); test03(); } }
反射操作注解
public class Test05 { public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException { Class<?> c1 = Class.forName("reflection.Customer"); // 通過(guò)反射獲取注解 Annotation[] annotations = c1.getAnnotations(); for (Annotation annotation:annotations){ System.out.println(annotation); } // 獲取注解的值 TableAnnotation annotation = c1.getAnnotation(TableAnnotation.class); System.out.println(annotation.value()); //獲取類(lèi)指定注解 Field id = c1.getDeclaredField("id"); FiledAnnotation annotation1 = id.getAnnotation(FiledAnnotation.class); System.out.println(annotation1.columnName()); System.out.println(annotation1.length()); System.out.println(annotation1.type()); } } //類(lèi)注解 @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @interface TableAnnotation{ String value(); } @Data @TableAnnotation("db_customer") class Customer { @FiledAnnotation(columnName="id",type = "Long",length =10) private Long id; @FiledAnnotation(columnName="age",type = "int",length =10) private int age; @FiledAnnotation(columnName="name",type = "String",length =10) private String name; } //方法注解 @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @interface FiledAnnotation{ String columnName(); String type(); int length(); }
到此這篇關(guān)于java高級用法之注解和反射的文章就介紹到這了,更多相關(guān)java注解和反射內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站