注解
什么是注解
代码演示
package com.test4;
public class Test01 extends Object {
@Override
public String toString() {
return super.toString();
}
}
内置注解
代码演示
package com.test4;
import java.util.ArrayList;
import java.util.List;
public class Test01 extends Object {
@Override
public String toString() {
return super.toString();
}
@Deprecated
public static void test02() {
System.out.println("@Deprecated");
}
@SuppressWarnings("all")
public void test01() {
List list=new ArrayList();
}
public static void main(String[] args) {
test02();
}
}
元注解
代码演示
package com.test4;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
public class Test02 {
@MyAnnotation
public void test1() {
}
}
@Target(value = {ElementType.METHOD,ElementType.TYPE})
@Retention(value=RetentionPolicy.RUNTIME)
@Documented
@Inherited
@interface MyAnnotation {
}
自定义注解
代码演示
package com.test4;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
public class Test3 {
@MyAnnottion(name = "张三", schools = "清华大学")
public void test() {
}
@MyAnnottion2("李四")
public void test2() {
}
}
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnottion {
String name() default "";
int age() default 0;
int id() default -1;
String[] schools();
}
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnottion2 {
String value();
}