对Java注解(Annotation)初步的认识
生活随笔
收集整理的這篇文章主要介紹了
对Java注解(Annotation)初步的认识
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
----------------------android培訓、java培訓、期待與您交流! ----------------------
為注解增加屬性
例如:
?
import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface AnnotationTest {
String color();
}
color()方法就是注解的屬性,他要求應用這個注解的類上的注解提供一個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());
}
}
}
有些注解的屬性在應用的時候可以不寫屬性和“=”,比如只寫value屬性的時候
比如:
?
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="";
?
?
為注解增加高級屬性
?
數組屬性
?
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 {
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培訓、java培訓、期待與您交流! ----------------------
詳細請查看:http://edu.csdn.net/heima
轉載于:https://www.cnblogs.com/1350995917ding/archive/2011/09/10/2173122.html
總結
以上是生活随笔為你收集整理的对Java注解(Annotation)初步的认识的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信公众号发送模板消息 -- PHP后台
- 下一篇: java++ioutils,Java I