黑马程序员技术交流社区
标题:
看过JAVA加强视频的进!求助
[打印本页]
作者:
Teale
时间:
2014-3-23 19:17
标题:
看过JAVA加强视频的进!求助
@ItcastAnnotation(color="blue")
public class AnnotationTest {
// @ItcastAnnotation(value="cc",color="red")
public static void main(String[] args) {
if (AnnotationTest.class.isAnnotationPresent(ItcastAnnotation.class)) {
ItcastAnnotation annotation = (ItcastAnnotation) Annotation.class.getAnnotation(ItcastAnnotation.class);
System.out.println(annotation.color());
}
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.TYPE })
public @interface ItcastAnnotation {
String color() default "blue";
// String value();
// int[] arrayAttr();
// Enumerous枚举.TrafficLamp lamp() default Enumerous枚举.TrafficLamp.RED;
// MetaAnnotation annotationAttr() default @MetaAnnotation("haha");
}
复制代码
关于给注解添加属性的。 我跟张老师写的一模一样,但是报错: java.lang.NullPointerException 。 昨天发安卓区没人找到,这里有大神么{:3_65:} 上面的导包和import 我就去掉了,Eclipse编译没有显示任何异常,而且annotation.color()这方法都是自动出来的。
作者:
Teale
时间:
2014-3-23 19:50
{:3_49:} 终于找到了。。。。。。。。。。。
ItcastAnnotation annotation = (ItcastAnnotation) Annotation.class.getAnnotation(ItcastAnnotation.class);
这里应该是AnnotationTest。。。伤不起找了两天
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2