@(Java知识点总结)[Java, 注解]java
使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口code
要点:继承
注意:接口
示例:字符串
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(value={ElementType.METHOD,ElementType.TYPE}) //既能够修饰方法,也能够修饰类 @Retention(RetentionPolicy.RUNTIME) //运行时 public @interface MyAnnotation { //使用@interface 关键字定义注解 String studentName(); //成员以无参无异常方式声明。方法的名称就是参数的名称 int age() default 18; // 能够使用default为成员指定一个默认值 String[] schools(); }
public class Demo2 { @MyAnnotation(age=21,schools={"浙江大学","清华大学"},studentName="张三") public void test1() { } }