| |
1. 13. 7. 单值语法 |
|
Using the single-value syntax when applying an annotation that has other members,
if other members all have default values. |
@interface SomeAnno {
int value();
int xyz() default 0;
}
@SomeAnno(88)
|
|
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
int value();
int defaultValue() default 100;
}
@MyAnnotation(102)
public class MainClass {
// Annotate a method.
@MyAnnotation(101)
public static void myMethod() {
}
public static void main(String[] arg) {
try {
MainClass ob = new MainClass();
Method m = ob.getClass( ).getMethod("myMethod");
Annotation[] annos = m.getAnnotations();
System.out.println("All annotations for myMeth:");
for(Annotation a : annos)
System.out.println(a);
} catch (Exception exc) {
}
}
}
|
|
All annotations for myMeth:
@MyAnnotation(defaultValue=100, value=101) |
|