- import java.lang.annotation.Annotation;
- @ItcastAnnotation(color="bule",value=3)
- public class AnnotationTest {
- public static void main(String[] args) {
- if(AnnotationTest.class.isAnnotationPresent(ItcastAnnotation.class)){
- ItcastAnnotation anno=(ItcastAnnotation)AnnotationTest.class.getAnnotation(ItcastAnnotation.class);
- System.out.println(anno);
- System.out.println(anno.color());
- //System.out.println(anno.lamp().nextLamp());
- System.out.println(anno.annotation().name());
-
- }
- }
- }
- /*打印结果:
- on
- on
- on
- @javaenhance.cn.day1.ItcastAnnotation(annotation=@javaenhance.cn.day1.meAnnotation(name=XXX), lamp=RED, color=bule, value=[3])
- bule
- XXX
- */
复制代码- import java.lang.annotation.ElementType;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
- @Retention(RetentionPolicy.RUNTIME)//生命周期
- @Target({ElementType.METHOD,ElementType.TYPE})//可以放置的位置。
- public @interface ItcastAnnotation {
- String color();
- int[] value();
- //LampText.Lump lamp() default LampText.Lump.RED;
- meAnnotation annotation() default @meAnnotation(name="XXX");//这里只有一个属性,但是为什么单写属性值会报错?
- }
复制代码- public @interface meAnnotation {
- String name();
- }
复制代码 2个问题。打印结果的3个on出自哪,单写属性值为什么报错?
|