国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

Java自定義注解及利用反射讀取注解

2019-11-14 15:06:35
字體:
供稿:網(wǎng)友

一、自定義注解

元注解: 

@interface注解: 定義注解接口

@Target注解: 用于約束被描述的注解的使用范圍,當(dāng)被描述的注解超出使用范圍則編譯失敗。如:ElementType.METHOD,ElementType.TYPE;

@Retention 注解:用于約束被定義注解的作用范圍,作用范圍有三個:

1,、RetentionPolicy.SOURCE:作用范圍是源碼,作用于java文件中,當(dāng)執(zhí)行javac時去除該注解。

2、RetentionPolicy.CLASS:作用范圍是二進(jìn)制碼,就是存在于class文件中,當(dāng)執(zhí)行Java時去除該注解。

3、RetentionPolicy.RUNTIME:作用范圍為運(yùn)行時,就是我們可以通過動態(tài)獲取該注釋。

@Documented:用于指定javadoc生成API文檔時顯示該注釋。

@Inherited:用于指定被描述的注釋可以被其描述的類的子類繼承,默認(rèn)情況是不能被其子類繼承。

自定義注解接口:

 

 1 package com.java.annotation; 2  3 import java.lang.annotation.Documented; 4 import java.lang.annotation.ElementType; 5 import java.lang.annotation.Inherited; 6 import java.lang.annotation.Retention; 7 import java.lang.annotation.RetentionPolicy; 8 import java.lang.annotation.Target; 9 10 @Target({ElementType.METHOD,ElementType.TYPE})11 @Inherited12 @Documented13 @Retention(RetentionPolicy.RUNTIME)14 public @interface Annotation_my {15     16     String name() default "張三";//defalt 表示默認(rèn)值17     18     String say() default "hello world";19     20     int age() default 21;21     22 }

 

接下來我們定義一個接口:

package com.java.annotation;@Annotation_my //使用我們剛才定義的注解public interface Person {        @Annotation_my    public void name();        @Annotation_my    public void say();        @Annotation_my    public void age();}

接口定義好了,我們就可以寫接口的實現(xiàn)類了(接口不能實例化)

然后我們就編寫一個測試類測試我們的注解

package com.java.annotation;import java.lang.annotation.Annotation;import java.lang.reflect.Field;import java.lang.reflect.Method;public class Text {    Annotation[] annotation = null;    public static void main(String[] args) throws ClassNotFoundException {        new Text().getAnnotation();    }        public void getAnnotation() throws ClassNotFoundException{        Class<?> stu = Class.forName("com.java.annotation.Student");//靜態(tài)加載類        boolean isEmpty = stu.isAnnotationPresent(com.java.annotation.Annotation_my.class);//判斷stu是不是使用了我們剛才定義的注解接口if(isEmpty){            annotation = stu.getAnnotations();//獲取注解接口中的            for(Annotation a:annotation){                Annotation_my my = (Annotation_my)a;//強(qiáng)制轉(zhuǎn)換成Annotation_my類型                System.out.println(stu+":/n"+my.name()+" say: "+my.say()+" my age: "+my.age());            }        }        Method[] method = stu.getMethods();//        System.out.println("Method");        for(Method m:method){            boolean ismEmpty = m.isAnnotationPresent(com.java.annotation.Annotation_my.class);            if(ismEmpty){                Annotation[] aa = m.getAnnotations();                for(Annotation a:aa){                    Annotation_my an = (Annotation_my)a;                    System.out.println(m+":/n"+an.name()+" say: "+an.say()+" my age: "+an.age());                }            }        }        //get Fields by force        System.out.println("get Fileds by force !");        Field[] field = stu.getDeclaredFields();        for(Field f:field){            f.setaccessible(true);            System.out.println(f.getName());        }        System.out.println("get methods in interfaces !");        Class<?> interfaces[] = stu.getInterfaces();        for(Class<?> c:interfaces){            Method[] imethod = c.getMethods();            for(Method m:imethod){                System.out.println(m.getName());            }        }    }}

 


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 大港区| 济南市| 富锦市| 厦门市| 彩票| 渝北区| 寿光市| 双辽市| 门头沟区| 平果县| 广平县| 如皋市| 清流县| 闸北区| 兴隆县| 延吉市| 宁阳县| 柳河县| 德保县| 长阳| 永兴县| 常州市| 富民县| 当涂县| 东乡族自治县| 上思县| 平安县| 丘北县| 云林县| 大安市| 桑日县| 巩留县| 陆良县| 凌源市| 高雄市| 沧源| 哈密市| 莱阳市| 加查县| 钟山县| 剑河县|