----------------------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