注解的定义及使用
(1)先自定义一些注解,学会自定义注解:
测试代码:
package annotationtest;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
public @interface MyAnnotation {
// 定义注解属性,看起来像个方法,实际上是注解的name属性
String name();
// 再定义一个int型属性
int id();
// 还可以给属性加默认值,这个属性用的时候可以不赋值,也可以不写,会自动赋默认值
int age() default 18;