注解
注解给程序读的注释
常用注解
`
`@Override 重写
@Deprecated 不建议使用
@SuppressWarning 警告删除
@SuppressWarnings("all") //警告删除
public class Test1 extends Object{
@Override //注解 重写的注解
public String toString() {
return super.toString();
}
@Deprecated //不推荐使用的方法
public static void test(){
System.out.println("test");
}
public static void run(){
test();
}
public static void main(String[] args) {
System.out.println("AA");
test();
}
}
元注解
//target 表示注解能用在哪些地方 类,方法,包等等
@Target(value = {ElementType.METHOD,ElementType.TYPE})
//表示注解在什么时候有效 runtime>class>source
@Retention(value = RetentionPolicy.RUNTIME)
//表示是否将注解生成在javadoc中
@Documented
//表示子类可以继承父类的注解
@Inherited
@interface MyAnnotation{
}
自定义注解
`
`@Target(value = {ElementType.METHOD})
@Retention(value = RetentionPolicy.RUNTIME)
@interface MyAnnotation2{
String name() default "";
int age() default 0;
int id() default -1; //默认值为-1,表示不存在
String[] addr();
}
@Target(value = ElementType.TYPE)
@Retention(value = RetentionPolicy.RUNTIME)
@interface MyAnnotation3{
}