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

首頁 > 學院 > 開發設計 > 正文

JAVA annotation入門

2019-11-17 04:09:20
字體:
來源:轉載
供稿:網友
一. 最常見的annotation
@Override:用在方法之上,用來告訴別人這一個方法是改寫父類的
@DePRecated:建議別人不要使用舊的API的時候用的,編譯的時候會用產生警告信息,可以設定在程序里的所有的元素上.
@SuppressWarnings:暫時把一些警告信息消息關閉
@Entity:表示該類是可持久化的類

二. 設計一個自己的Annotation
      先看代碼再講話
1. 只有一個參數的Annotation實現

view plaincopy to clipboardprint?
package chb.test.annotation;    
import java.lang.annotation.Documented;    
import java.lang.annotation.ElementType;    
import java.lang.annotation.Retention;    
import java.lang.annotation.RetentionPolicy;    
import java.lang.annotation.Target;    
@Target(ElementType.TYPE)    
@Retention(RetentionPolicy.RUNTIME)    
@Documented   
public @interface MyAnnotation1 {    
        String value();    
}   
view plaincopy to clipboardprint?
package chb.test.annotation;   
import java.lang.annotation.Documented;   
import java.lang.annotation.ElementType;   
import java.lang.annotation.Retention;   
import java.lang.annotation.RetentionPolicy;   
import java.lang.annotation.Target;   
@Target(ElementType.TYPE)   
@Retention(RetentionPolicy.RUNTIME)   
@Documented  
public @interface MyAnnotation1 {   
        String value();   
}  
package chb.test.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation1 {
        String value();
}

2. 有兩個參數的Annotation實現

view plaincopy to clipboardprint?
package chb.test.annotation;    
import java.lang.annotation.Documented;    
import java.lang.annotation.ElementType;    
import java.lang.annotation.Retention;    
import java.lang.annotation.RetentionPolicy;    
import java.lang.annotation.Target;    
@Target(ElementType.METHOD)    
@Retention(RetentionPolicy.RUNTIME)    
@Documented   
public @interface MyAnnotation2 {    
        String description();    
        boolean isAnnotation();    
}   
view plaincopy to clipboardprint?
package chb.test.annotation;   
import java.lang.annotation.Documented;   
import java.lang.annotation.ElementType;   
import java.lang.annotation.Retention;   
import java.lang.annotation.RetentionPolicy;   
import java.lang.annotation.Target;   
@Target(ElementType.METHOD)   
@Retention(RetentionPolicy.RUNTIME)   
@Documented  
public @interface MyAnnotation2 {   
        String description();   
        boolean isAnnotation();   
}  
package chb.test.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation2 {
        String description();
        boolean isAnnotation();
}

3. Annotation實驗類

view plaincopy to clipboardprint?
package chb.test.annotation;    
@MyAnnotation1("this is annotation1")    
public class AnnotationDemo {    
        @MyAnnotation2(description="this is annotation2",isAnnotation=true)    
        public void sayHello(){    
                System.out.println("hello world!");    
        }    
}   
view plaincopy to clipboardprint?
package chb.test.annotation;   
@MyAnnotation1("this is annotation1")   
public class AnnotationDemo {   
        @MyAnnotation2(description="this is annotation2",isAnnotation=true)   
        public void sayHello(){   
                System.out.println("hello world!");   
        }   
}  
package chb.test.annotation;
@MyAnnotation1("this is annotation1")
public class AnnotationDemo {
        @MyAnnotation2(description="this is annotation2",isAnnotation=true)
        public void sayHello(){
                System.out.println("hello world!");
        }
}

4.Annotation測試說明類

view plaincopy to clipboardprint?
package chb.test.annotation;    
import java.lang.reflect.Method;    
import org.junit.Test;    
public class TestAnnotation {    
        @Test   
        public void test() throws ClassNotFoundException, SecurityException, NoSuchMethodException{    
                Class<?> cls = Class.forName("chb.test.annotation.AnnotationDemo");    
                boolean flag = cls.isAnnotationPresent(MyAnnotation1.class);    
                if(flag){    
                        System.out.println("判斷類是annotation");    
                        MyAnnotation1 annotation1 = cls.getAnnotation(MyAnnotation1.class);    
                        System.out.println(annotation1.value());    
                }    
                    
                Method method = cls.getMethod("sayHello");    
                flag = method.isAnnotationPresent(MyAnnotation2.class) ;    
                if(flag){    
                        System.out.println("判斷方法也是annotation");    
                        MyAnnotation2 annotation2 = method.getAnnotation(MyAnnotation2.class);    
                        System.out.println(annotation2.description()+"/t"+annotation2.isAnnotation());    
                }    
        }    
            
}   
view plaincopy to clipboardprint?
package chb.test.annotation;   
import java.lang.reflect.Method;   
import org.junit.Test;   
public class TestAnnotation {   
        @Test  
        public void test() throws ClassNotFoundException, SecurityException, NoSuchMethodException{   
                Class<?> cls = Class.forName("chb.test.annotation.AnnotationDemo");   
                boolean flag = cls.isAnnotationPresent(MyAnnotation1.class);   
                if(flag){   
                        System.out.println("判斷類是annotation");   
                        MyAnnotation1 annotation1 = cls.getAnnotation(MyAnnotation1.class);   
                        System.out.println(annotation1.value());   
                }   
                   
                Method method = cls.getMethod("sayHello");   
                flag = method.isAnnotationPresent(MyAnnotation2.class) ;   
                if(flag){   
                        System.out.println("判斷方法也是annotation");   
                        MyAnnotation2 annotation2 = method.getAnnotation(MyAnnotation2.class);   
                        System.out.println(annotation2.description()+"/t"+annotation2.isAnnotation());   
                }   
        }   
           
}  
package chb.test.annotation;
import java.lang.reflect.Method;
import org.junit.Test;
public class TestAnnotation {
        @Test
        public void test() throws ClassNotFoundException, SecurityException, NoSuchMethodException{
                Class<?> cls = Class.forName("chb.test.annotation.AnnotationDemo");
                boolean flag = cls.isAnnotationPresent(MyAnnotation1.class);
                if(flag){
                        System.out.println("判斷類是annotation");
                        MyAnnotation1 annotation1 = cls.getAnnotation(MyAnnotation1.class);
                        System.out.println(annotation1.value());
                }
                
                Method method = cls.getMethod("sayHello");
                flag = method.isAnnotationPresent(MyAnnotation2.class) ;
                if(flag){
                        System.out.println("判斷方法也是annotation");
                        MyAnnotation2 annotation2 = method.getAnnotation(MyAnnotation2.class);
                        System.out.println(annotation2.description()+"/t"+annotation2.isAnnotation());
                }
        }
        
}

實驗結果,控制臺打出如下信息:

判斷類是annotation
this is annotation1
判斷方法也是annotation
this is annotation2     true

三.簡介及說明

1. MyAnnotation1中的@Target(ElementType.TYPE)
      @Target里面的ElementType是用來指定Annotation類型可以用在哪些元素上的.例如:
       TYPE(類型)、FIELD(屬性)、METHOD(方法)、PARAMETER(參數)、CONSTRUCTOR(構造函數)、LOCAL_VARIABLE(局部變量),、PACKAGE(包),其中的TYPE(類型)是指可以用在Class,Interface,Enum和Annotation類型上。
2. MyAnnotation1中的@Retention(RetentionPolicy.RUNTIME)
      RetentionPolicy 共有三種策略,分別為:
SOURCE:這個Annotation類型的信息只會保留在程序源碼里,源碼如果經過了編譯之后,Annotation的數據就會消失,并不會保留在編譯好的.class文件里面
CLASS:這個Annotation類型的信息保留在程序源碼里,同時也會保留在編譯好的.class文件里面,在執行的時候,并不會把這些信息加載到JVM中。注:默認策略為CLASS類型
RUNTIME:表示在源碼、編譯好的.class文件中保留信息,在執行的時候會把這一些信息加載到JVM中去的
3. MyAnnotation1中的@Documented
目的就是將這一Annotation的信息顯示在JAVA API文檔上,如果沒有增加@Documented的話,JAVA API文檔上不會顯示相關annotation信息

4. MyAnnotation1中的@interface
   關鍵字,表示該類為Annotation定義
5. MyAnnotation1中的 String value();
   表示有一個成員參數,名字為value,訪問權為默認(default)修飾符,注意以下兩點:
訪問權只能用public和默認(default)修飾
參數成員只能用基本類型byte,short,char,int,long,float,double,boolean八種基本數據類型和String,Enum,Class,annotations等數據類型,以及這一些類型的數組
6.AnnotationDemo中的@MyAnnotation1("this is annotation1")
   因為MyAnnotation1只有一個參數,因此可以直接在括號中寫上value值。注:如果Annotation只有一個參數,則建議最好將該參數名稱定義為value
7.TestAnnotation中的cls.isAnnotationPresent(MyAnnotation1.class)
    判斷該類是否使用了MyAnnotation1的注釋
8. TestAnnotation中的MyAnnotation1 annotation1 = cls.getAnnotation(MyAnnotation1.class)
    返回該類針對MyAnnotation1的注釋
9. TestAnnotation中的method.isAnnotationPresent(MyAnnotation2.class)
    判斷該方法是否使用了MyAnnotation2的注釋
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 张家口市| 巫山县| 安徽省| 奉贤区| 石家庄市| 定襄县| 黄平县| 德州市| 隆安县| 镇宁| 循化| 周宁县| 东莞市| 萨迦县| 宜君县| 偏关县| 曲水县| 丰都县| 沂水县| 韶关市| 贵州省| 根河市| 蕉岭县| 滨州市| 榕江县| 东兴市| 博客| 泰宁县| 高青县| 安阳市| 彩票| 乌鲁木齐县| 房产| 改则县| 义乌市| 阿荣旗| 博野县| 龙井市| 邵武市| 武鸣县| 石狮市|