Java8注解語(yǔ)法有哪些-創(chuàng)新互聯(lián)

這篇文章主要講解了“Java 8注解語(yǔ)法有哪些”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“Java 8注解語(yǔ)法有哪些”吧!

成都創(chuàng)新互聯(lián)為客戶提供專業(yè)的成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、成都外貿(mào)網(wǎng)站建設(shè)、程序、域名、空間一條龍服務(wù),提供基于WEB的系統(tǒng)開發(fā). 服務(wù)項(xiàng)目涵蓋了網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站程序開發(fā)、WEB系統(tǒng)開發(fā)、微信二次開發(fā)、手機(jī)網(wǎng)站開發(fā)等網(wǎng)站方面業(yè)務(wù)。

注解語(yǔ)法

注解由字符 @ 和注解名組成,即 @AnnotationName。當(dāng)編譯器發(fā)現(xiàn)這個(gè)語(yǔ)法該元素時(shí),會(huì)把它解析為注解。例如:

@ExampleAnnotation
public class SampleClass {
}

上面的注解稱為 ExampleAnnotation,標(biāo)記在 SampleClass 上。

注解可以包含屬性,在聲明注解時(shí)以鍵值對(duì)的形式給出。例如: 

@ExampleAnnotation(name = ”first name”, age = 
35)
public void simpleMethod() {
}

請(qǐng)注意,這里 ExampleAnnotation 是一個(gè)方法注解。如果注解只包含一個(gè)屬性,在聲明注解時(shí)可以忽略屬性名。示例如下:

@ExampleAnnotation(“I am the only property”)
public void simpleMethod() {
}

一個(gè)元素可以使用多個(gè)注解。比如下面這個(gè)示例:

@Annotation1
@Annotation2(“Another Annotation”)
public class SimpleClass {
}

從 J2SE 8 開始,可以為同一個(gè)元素多次使用相同的注解,例如:

@ExampleAnnotation(“Annotation used”)
@ExampleAnnotation(“Annotation repeated”)
public class SimpleClass {
}

在 @Repeatable 部分會(huì)對(duì)此進(jìn)行詳細(xì)地討論。

Java 預(yù)定義注解

Java 支持一組預(yù)先定義好的注解。下面介紹了Java Core 中提供的注解:

@Retention:

  • SOURCE:注解僅在源代碼中可用。編譯器和 JVM 會(huì)忽略此注解,因此在運(yùn)行時(shí)不可用;

  • CLASS:編譯器會(huì)處理該注解,但 JVM 不會(huì)處理,因此在運(yùn)行時(shí)不可用;

  • RUNTIME:JVM 會(huì)處理該注解,可以在運(yùn)行時(shí)使用。

@Target:

  • ANNOTATION_TYPE:可修飾其他注解;

  • CONSTRUCTOR:可以修飾構(gòu)造函數(shù);

  • FIELD:可以修飾字段或?qū)傩裕?/p>

  • LOCAL_VARIABLE:可以修飾局部變量;

  • METHOD:可以修飾 method;

  • PACKAGE:可以修飾 package 聲明;

  • PARAMETER:可以修飾方法參數(shù);

  • TYPE:可以修飾 Class、Interface、Annotation 或 enum 聲明;

  • PACKAGE:可以修飾 package 聲明;

  • TYPE_PARAMETER:可以修飾參數(shù)聲明;

  • TYPE_USE:可以修飾任何類型。

@Documented:@Retention (RetentionPolicy.RUNTIME)
@Target (ElementType.TYPE_USE)
@Repeatable (RepeatableAnnotationContainer.class)
public @interface RepeatableAnnotation() {
   String values();
}

RepeatableAnnotation可以重復(fù)修飾元素。

接下來(lái),定義 RepeatableAnnotationContainer注解類型。這是一個(gè)注解類型容器,包含一個(gè) RepeatableAnnotation 類型的數(shù)組。

public @interface RepeatableAnnotationContainer {
   RepeatableAnnotation [] value();
}

現(xiàn)在,Repeatable 可以元素進(jìn)行多次注釋。

@RepeatableAnnotation (“I am annotating the class”)
@RepeatableAnnotation (“I am annotating the class again”)
@RepeatableAnnotation (“I am annotating the classfor the third time”)
publicclass RepeatedAnnotationExample {function(){   //外匯跟單www.gendan5.com}

在程序中要獲取注解中的值,先要獲取容器中的數(shù)組,數(shù)組的每個(gè)元素將包含一個(gè)值。例如:

@RepeatableAnnotation (“I am annotating the 
class”)
@RepeatableAnnotation (“I am annotating the class again”)
@RepeatableAnnotation(“I am annotating the class for the third time”)
public class RepeatableAnnotationExample {
   public static void main(String [] args) {
       Class object = RepeatableAnnotationExample.class
       Annotation[] annotations = object.getAnnotations();
for (Annotation annotation : annotations) {
   RepeatableAnnotationContainer rac = (RepeatableAnnotationContainer) annotation;
   RepeatableAnnotation [] raArray = rac.value();
   for (RepeatableAnnotation ra : raArray) {
       System.out.println(ra.value);
   }
}
}
}

執(zhí)行結(jié)果:

I am annotating the 
class
I am annotating the class again
I am annotating the class for the third time.

類型注解

Java 8發(fā)布后,注解可以用于任何類型(Type),這意味著只要可以使用類型的地方就能使用注解。例如,使用新運(yùn)算符創(chuàng)建類實(shí)例、類型轉(zhuǎn)換、用 implements 實(shí)現(xiàn)接口、拋出異常等,這種注解稱為類型注解。 

這種注解能夠幫助分析與改進(jìn) Java 程序,提供更強(qiáng)大的類型檢查。Java 8發(fā)布前,Java 沒有提供類型檢查框架。但是通過(guò)類型注解可以開發(fā)類型檢查框架,對(duì) Java 程序進(jìn)行檢查。

舉例來(lái)說(shuō),假設(shè)我們希望特定變量在程序執(zhí)行過(guò)程中始終不為 null??梢跃帉懸粋€(gè)自定義插件 NonNull,并為特定變量加上該注解進(jìn)行檢查。變量聲明如下:

@NonNull String notNullString;

編譯代碼時(shí),如果發(fā)現(xiàn)任何可能將變量賦值為 null 的代碼,編譯器會(huì)檢查潛在問(wèn)題并給出告警。

自定義注解

Java 允許程序員自定義注解。自行定義注解的語(yǔ)法:

public @interface CustomAnnotation { }

上面的代碼會(huì)創(chuàng)建一個(gè) CustomAnnotation新注解。@Interface 關(guān)鍵字可用于自定義注解。

自定義注解時(shí),必須設(shè)置兩個(gè)必填屬性??梢栽诙x中增加其他屬性,但這兩個(gè)重要屬性是必需的,即@Retention (RetentionPolicy.RUNTIME)
@Target (ElementType.ELEMENT)
public @interface CustomAnnotation {
   public String name() default “Mr Bean”;
   public String dateOfBirth();
}

上面的自定義注解中,Retention Policy 為 RUNTIME,這表明該注解可以在 JVM 運(yùn)行時(shí)使用;Target 設(shè)為 ELEMENT,表示該注解可以修飾任何元素與類型。 

此外,它還具有兩個(gè)屬性:name 與 dateOfBirth。其中,name 屬性默認(rèn)值為 Mr Bean, dateOfBirth 沒有默認(rèn)值。

注意,聲明的 Method 沒有帶任何參數(shù)以及 throws 語(yǔ)句。此外,返回類型僅限于 String、class、enum、注解以及上述類型的數(shù)組。

現(xiàn)在,可以像下面這樣使用自定義注解:

@CustomAnnotation (dateOfBirth = “1980-06-25”)
public class CustomAnnotatedClass {
}

同樣,可以使用 @Target(ElementType.METHOD) 創(chuàng)建自定義注解修飾 method。

獲取注解及屬性

Java Reflection API 提供了幾種方法,可以在運(yùn)行時(shí)中從 class、method 和其他元素中獲取注解。 

AnnotatedElement接口定義了所有的方法,其中最重要的一個(gè)是:

  • getAnnotations(): 返回指定元素的所有注解,包括定義元素時(shí)未明確寫出的注解。

  • isAnnotationPresent(annotation): 檢查注解在當(dāng)前元素上是否可用。

  • getAnnotation(class): 獲取 class 參數(shù)使用的注解,如果參數(shù)不存在注解返回 null。

這個(gè) class 支持 java.lang.Class、java.lang.reflect.Method 和 java.lang.reflect.Field,基本上可以適用任何的 Java 元素。

下面的示例程序展示了如何獲取自定義注解的相關(guān)信息:

public static void main(String [] args) {
Class object = CustomAnnotatedClass.class;
// 從類中獲取所有注解
Annotation[] annotations = object.getAnnotations();
for( Annotation annotation : annotations ) {
System.out.println(annotation);
}
// 檢查是否存在注解
if( object.isAnnotationPresent( CustomAnnotationClass.class ) ) {
// 獲取需要的注解
Annotation annotation = object.getAnnotation(CustomAnnotationClass.class) ;
System.out.println(annotation);
}
// 獲取注解屬性
for(Annotation annotation : annotations) {
System.out.println(“name: “ + annotation.name());
System.out.println(“Date of Birth: “+ annotation.dateOfBirth());
}
// 對(duì)所有方法執(zhí)行相同的操作
for( Method method : object.getDeclaredMethods() ) {
if( method.isAnnotationPresent( CustomAnnotationMethod.class ) ) {
Annotation annotation = method.getAnnotation(CustomAnnotationMethod.class );
System.out.println( annotation );
}
}
}

感謝各位的閱讀,以上就是“Java 8注解語(yǔ)法有哪些”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)Java 8注解語(yǔ)法有哪些這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

文章標(biāo)題:Java8注解語(yǔ)法有哪些-創(chuàng)新互聯(lián)
轉(zhuǎn)載來(lái)源:http://muchs.cn/article10/djjggo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、響應(yīng)式網(wǎng)站、虛擬主機(jī)、ChatGPT、做網(wǎng)站、關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)