黑马程序员_第七天_高新技术之注解入门和应用

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值