1.1 什么是注解
注解起到规范和约束的作用。
@Override //重写注解
@Deprecated //废弃,不推荐程序员使用,但是可以使用,或者存在更好的方式
1.2 内置注解
package annotation;
//镇压警告,这是一个可以放置参数的注解
@SuppressWarnings("all")
public class Test01 extends Object{
@Override
public String toString() {
return super.toString();
}
@Deprecated
public static void test(){
System.out.println("Deprecated");
}
public static void main(String[] args) {
test();
}
}
1.3 元注解
target:类上、方法上、字段上
package annotation;
import javax.xml.bind.Element;
import java.lang.annotation.*;
//测试元注解
public class Test02 {
}
//定义一个注解
@Target(value ={ElementType.METHOD,ElementType.TYPE})
//Retention 表示我们的注解在什么地方还有效
//runtime>class>source
@Retention(value= RetentionPolicy.RUNTIME )
//Documented 表示是否将我们的注解生成在Javadoc中
@Documented
//Inherited 子类可以继承父类中的该注解
@Inherited
@interface MyAnnotation{
}