---------------------- <a href="http://edu.csdn.net/heima" target="blank">android培训</a>、<a href="http://edu.csdn.net/heima" target="blank">java培训</a>、期待与您交流! ----------------------
今天学习的内容是注解,以前头脑中的注解就是加两个斜线的文字说明。在看了视频后才明白:注解相当于一种标记,加了注解就等于打上了某种标记。例如:@SuppressWarnings,@Deprecated,@Override等等这些都是。如果在开发时想忽略旧方法那么可以用@SuppressWarnings("deprecation"),如果需要重写父类方法,就用@Override。标记可以加在包,类,字段,方法,方法的参数以及局部变量上。
在反射中的应用:
if(AnnotationTest.class.isAnnotationPresent(ItcastAnnotation.class)){
ItcastAnnotation annotation = (ItcastAnnotation)AnnotationTest.class.getAnnotation(ItcastAnnotation.class);
System.out.println(annotation);
}
}
public @interface ItcastAnnotation{}
可以为注解添加各种属性:
String color();
@MyAnnotation(color="red")
MyAnnotation a = (MyAnnotation)AnnotationTest.class.getAnnotation(MyAnnotation.class);
System.out.println(a.color());
String color() default "yellow";
String color() default "zxx";
还有自定义的注解:
public @interface MyAnnotation{},把它加在某个类上:
@MyAnnotation public class AnnotationTest{}。其中引出
元注解的三种取值:
java源文件->RetentionPolicy.SOURCE;
class文件->RetentionPolicy.CLASS;
内存中的字节码->RetentionPolicy.RUNTIME
@Target元注解:默认值为任何元素,数组方式设置{ElementType.METHOD,ElementType.TYPE}
没想到这就是注解,而且它的作用还这么大,以后用到它的地方很多,争取一下搞定。
---------------------- <a href="http://edu.csdn.net/heima" target="blank">android培训</a>、<a href="http://edu.csdn.net/heima" target="blank">java培训</a>、期待与您交流! ----------------------详细请查看:<a href="http://edu.csdn.net/heima" target="blank">http://edu.csdn.net/heima</a>