欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
Tiger系列四:Annotation第二部分:定制Annotation
作者:http://blog.csdn.net/chenyun2000/archive/2005/02/05/281824.aspx
1
、自定義Annotation類(lèi)型

1)定義Annotation類(lèi)型

l         使用@interface聲明Annotation類(lèi)型

public @interface InProgress {
 
}

l         使用Annotation類(lèi)型

public class TestAnnotation {
       @InProcess
       public void test() {
         
       }
}

l         如果Annotation類(lèi)型和使用它的類(lèi)不在相同的包中,可以import Annotation類(lèi)型,以便直接使用 @InProgress

2)添加成員

l         Annotation類(lèi)型可以有成員變量,以提供有用的信息

l         定義數據成員不需要定義gettersetter方法,只需要定義一個(gè)以成員名稱(chēng)命名的方法,并指定返回類(lèi)型為需要的數據類(lèi)型

l         簡(jiǎn)單的例子:

public @interface TODO {
       String value();
}

l         使用帶成員的Annotation類(lèi)型:

public class TestAnnotation {
       @InProcess
       @TODO("Need to finish this method later")
       public void test() {
         
       }
}

3)設置缺省值

l         要為Annotation類(lèi)型的成員設置缺省值,需要在聲明成員時(shí)使用default關(guān)鍵字:

public @interface GroupTODO {
       public enum Severity {
              CRITICAL, IMPORTANT, TRIVIAL, DOCUMENTATION
       };
 
       Severity severity() default Severity.IMPORTANT;
       String item();
       String assignedTo();
       String dateAssigned();
}

l         當然,缺省值的類(lèi)型必須與成員變量聲明的類(lèi)型完全相同

l         下面是使用缺省值的例子:

public class TestAnnotation {
       @InProcess
       @GroupTODO(
              item="Need to finish this method later",
              assignedTo="nelson_tu",
              dateAssigned="2005/02/05"
       )
       public void test() {
         
       }
}

l         下面是改寫(xiě)缺省值的例子:

public class TestAnnotation {
       @InProcess
       //@TODO("Need to finish this method later")
       @GroupTODO(
              severity=GroupTODO.Severity.DOCUMENTATION,
              item="Need to finish this method later",
              assignedTo="nelson_tu",
              dateAssigned="2005/02/05"
       )
       public void test() {
         
       }
}

 

2、元Annotation

l         Annotation就是AnnotationAnnotation,JDK5提供了4種預定義的元Annotation

1@Target

l         @Target指定Annotation類(lèi)型可以應用的程序元素,以便在其它程序元素中誤用Annotation類(lèi)型

l         程序元素的類(lèi)型由java.lang.annotation.ElementType枚舉類(lèi)定義:

package java.lang.annotation;
 
public enum ElementType {
  TYPE,          // Class, interface, or enum (but not annotation)
  FIELD,         // Field (including enumerated values)
  METHOD,        // Method (does not include constructors)
  PARAMETER,             // Method parameter
  CONSTRUCTOR,           // Constructor
  LOCAL_VARIABLE, // Local variable or catch clause
  ANNOTATION_TYPE,       // Annotation Types (meta-annotations)
  PACKAGE        // Java package
}

l         下面是使用@Target的例子:

@Target({ElementType.TYPE,
    ElementType.METHOD,
    ElementType.CONSTRUCTOR,
    ElementType.ANNOTATION_TYPE})
public @interface TODO {
       String value();
}

2@Retention

l         @Retention Java 編譯器處理Annotation類(lèi)型的方式有關(guān)

l         這些方式由java.lang.annotation.RetentionPolicy 枚舉類(lèi)定義:

package java.lang.annotation;
 
public enum RetentionPolicy {
  SOURCE,       // Annotation is discarded by the compiler
  CLASS,       // Annotation is stored in the class file, but ignored by the VM
  RUNTIME       // Annotation is stored in the class file and read by the VM
}

l         使用@Retention的例子參看后面的@Documented

3@Documented

l         @Documented指明需要在Javadoc中包含Annotation(缺省是不包含的)

l         下面是一個(gè)使用@Documented的例子:

@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface InProcess {
 
}

l         使用@Documented的一個(gè)技巧就是指定保持性策略為RetentionPolicy.RUNTIME:這樣,Annotation就會(huì )保留在編譯后的類(lèi)文件中并且由虛擬機加載,然后Javadoc就可以抽取出Annotation,添加到類(lèi)的HTML文檔中

4@Inherited

l         @Inherited最復雜、使用最少、也最容易造成混淆的一個(gè)

l         假設使用@InProgress 標記一個(gè)正在開(kāi)發(fā)的類(lèi),只要正確應用@Documented,Annotation信息就會(huì )出現在Javadoc中;現在要編寫(xiě)一個(gè)新類(lèi),擴展那個(gè)正在開(kāi)發(fā)的類(lèi),那么使用子類(lèi),或者查看它的文檔,根本沒(méi)法表明還有什么地方?jīng)]有完成;而本來(lái)是希望@InProgress Annotation信息會(huì )被帶到子類(lèi)中,這就需要使用@Inherited

l         下面是這樣的例子:

@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface InProcess {
 
}

 


本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
Java Annotation詳解
秒懂 Java注解類(lèi)型(@Annotation)
JAVA 基礎篇-注解的概念
面試官:說(shuō)說(shuō)你對【注解】的理解
@interface java注解
Java Annotation之介紹篇 - 開(kāi)心就好! - JavaEye技術(shù)網(wǎng)站
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久