内置注解:
@Override:重写父类的方法上使用
//@Override 重写的注解
@Override
public String toString() {
return super.toString();
}
@Deprecated:不推荐使用但能使用的方法上注释
//@Deprecated 过时的注解, 表示 所注解的方法过时了,不推荐使用,但可以使用该方法
@Deprecated
public static void test(){
System.out.println("Deprecated");
}
@SuppressWarnings("all"):忽略类或方法中的所有警告
@SuppressWarnings("all")
public void test2() {
List list=new ArrayList();
}
元注解:负责注解其它注解
@Target:表示注解可以用在哪些地方
@Retention:表示被修饰的注解在什么地方有效
@Documented:表示是否将注解生成在Javadoc中
@Inherited:表示是否允许子类继承该注解
//定义一个注解
//Target:表示注解可以用在哪些地方
@Target(value= {ElementType.METHOD,ElementType.TYPE})
//Retention:表示被修饰的注解在什么地方有效
@Retention(value= RetentionPolicy.RUNTIME)
//Documented:表示是否将注解生成在Javadoc中
@Documented
//Inherited:表示是否允许子类继承该注解
@Inherited
@interface MyAnnotation{
}
自定义注解
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2 {
//注解的参数:参数类型+参数名();
String name();
int age() default 0;
int id() default -1;//若默认值为-1,代表不存在
String [] schools() default {"建大","建小"};
}
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation3 {
//注解只有一个值,可以使用value命名
String value();
}