日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

对Java注解(Annotation)初步的认识

發(fā)布時(shí)間:2023/12/20 java 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 对Java注解(Annotation)初步的认识 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

----------------------android培訓(xùn)、java培訓(xùn)、期待與您交流! ----------------------

為注解增加屬性

例如:

?

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)

public @interface AnnotationTest {

String color();
}

color()方法就是注解的屬性,他要求應(yīng)用這個(gè)注解的類上的注解提供一個(gè)String類型的返回值。

比如:

?

@AnnotationTest(color ="red")
publicclass TestAnnotation {

publicstaticvoid main(String[] args) {
if(TestAnnotation.class.isAnnotationPresent((AnnotationTest.class))){
AnnotationTest test = (AnnotationTest)TestAnnotation.class.getAnnotation(AnnotationTest.class);
System.out.println(test.color());

}
}
}

有些注解的屬性在應(yīng)用的時(shí)候可以不寫(xiě)屬性和“=”,比如只寫(xiě)value屬性的時(shí)候

比如:

?

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)

public @interface AnnotationTest {
String value();
}

@AnnotationTest("red")
publicclass TestAnnotation {
publicstaticvoid main(String[] args) {
if(TestAnnotation.class.isAnnotationPresent((AnnotationTest.class))){
AnnotationTest test = (AnnotationTest)TestAnnotation.class.getAnnotation(AnnotationTest.class);
System.out.println(test.value());
}
}
}

還可以為屬性指定缺省值:

?

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)

public @interface AnnotationTest {

String color() default"blue";
String value();
}

@AnnotationTest("red")
publicclass TestAnnotation {

publicstaticvoid main(String[] args) {
if(TestAnnotation.class.isAnnotationPresent((AnnotationTest.class))){
AnnotationTest test = (AnnotationTest)TestAnnotation.class.getAnnotation(AnnotationTest.class);
System.out.println(test.color());
System.out.println(test.value());
}
}

}

如果不指定color()缺省@AnnotationTest()中既不能省略value=,也不能省略color="";

?

?

為注解增加高級(jí)屬性

?

數(shù)組屬性

?

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)

public @interface AnnotationTest {
int[] intArray();
}


@AnnotationTest(intArray={1,2,3})

publicclass TestAnnotation {

publicstaticvoid main(String[] args) {
if(TestAnnotation.class.isAnnotationPresent((AnnotationTest.class))){
AnnotationTest test = (AnnotationTest)TestAnnotation.class.getAnnotation(AnnotationTest.class);
for(int i:test.intArray()){
System.out.println(i);
}
}
}

}

如果數(shù)組的屬性值只有一個(gè)時(shí),賦值時(shí)可以省略大括號(hào)

?

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)

public @interface AnnotationTest {
int[] intArray();
}


@AnnotationTest(intArray={1,2,3})

publicclass TestAnnotation {

publicstaticvoid main(String[] args) {
if(TestAnnotation.class.isAnnotationPresent((AnnotationTest.class))){
AnnotationTest test = (AnnotationTest)TestAnnotation.class.getAnnotation(AnnotationTest.class);
for(int i:test.intArray()){
System.out.println(i);
}
}
}

}

枚舉屬性

?

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)

public @interface AnnotationTest {
enum color{red,blue}
color value();
}
@AnnotationTest(color.red)
publicclass TestAnnotation {}

----------------------android培訓(xùn)java培訓(xùn)、期待與您交流! ----------------------

詳細(xì)請(qǐng)查看:http://edu.csdn.net/heima

轉(zhuǎn)載于:https://www.cnblogs.com/1350995917ding/archive/2011/09/10/2173122.html

總結(jié)

以上是生活随笔為你收集整理的对Java注解(Annotation)初步的认识的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。