---------------------- <a href="http://edu.youkuaiyun.com"target="blank">ASP.Net+Android+IOS开发</a>、<a href="http://edu.youkuaiyun.com"target="blank">.Net培训</a>、期待与您交流! ----------------------
---@Override 方法上面 判断是否重写
---@Deprecated 方法上面 表示方法废弃不建议被使用
---@SuppressWarnings("deprecation") 压制警告(废弃的警告)
2.学会自己写注解
---注解跟类接口同级 书写方式public @interface ItcastAnnotation
---在注解上定义注解
// 元注解 元数据 元信息 retention表示3中类型 class source runtime// target 表示注解的作用范围 比如 method 方法上面 type 类接口 注解枚举上面@Target({ ElementType.METHOD, ElementType.TYPE })@Retention(RetentionPolicy.RUNTIME)
这两个注解定义元素都是 枚举类型
public @interface Target { ElementType[] value(); } public @interface Retention { RetentionPolicy value(); }
一个注解实例
public @interface ItcastAnnotation {
String color() default "blue";
// 特殊属性比如 @SuppressWarnings("deprecation") 就是等于value=“deprecation”
// 但只有String value(); 时才能用 如果还有 String color() 但还想用就要设定默认值default "blue";
// 支持 @ItcastAnnotation("qixing")与
// @ItcastAnnotation(color="red",value="qixing")
String value();
// 定义数组属性
int[] arrayAttr() default { 3, 4, 5 };
// 可以是枚举
/*
* public @interface Target { ElementType[] value(); }
* public @interface Retention { RetentionPolicy value(); }
*/
//还可以是annotation
MyAnnotation annotationAttr() default @MyAnnotation("qixing");
}
注解的使用
@ItcastAnnotation(color="red",value="qixing",annotationAttr = @MyAnnotation("yinhe"))
public class AnnotationTest {
//注解实例
//压制警告
@SuppressWarnings("deprecation")
public static void main(String[] args) {
/* System.runFinalizersOnExit(true);
System.out.println("nihao");*/
if ( AnnotationTest.class.isAnnotationPresent(ItcastAnnotation.class)) {
ItcastAnnotation anno = AnnotationTest.class.getAnnotation(ItcastAnnotation.class);
System.out.println(anno);
System.out.println(new Person());
System.out.println(anno.color());
System.out.println(anno.value());
System.out.println(anno.arrayAttr().length);
System.out.println(anno.annotationAttr().value());
}
}}
注解是什么呢 比如方法上的注解
1.ItcastAnnotation anno = AnnotationTest.class.getAnnotation(ItcastAnnotation.class); 得到方法上单注解名称
2.通过反射class.forname()得到 注解对象
3.比如3大基本注解 其实就是得到名字 jvm判断对象名字是否与override相等 如果相当就去执行判断是否重写的方法
---------------------- <a href="http://edu.youkuaiyun.com"target="blank">ASP.Net+Android+IOS开发</a>、<a href="http://edu.youkuaiyun.com"target="blank">.Net培训</a>、期待与您交流! ----------------------