java注解(Annotation)
自定义注解DEMO:
/*package com.kuang.reflection;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@MyAnnotation(id = 0, name = "dd")
public class AnnotationDemo {
@MyAnnotation(id = 0, name = "cc")
private int age;
@MyAnnotation(id = 0, name = "aa")
public void printAge() {
System.out.println("age is: " + age);
}
}
@Target 指定注解作用的的位置
ElementType.TYPE :注解作用在类上
ElementType.METHOD:注解作用在方法上
ElementType.FIELD:注解作用在属性上
@Retention:指定何时有效
RetentionPolicy.RUNTIME:运行时有效
//ElementType.TYPE,ElementType.METHOD,ElementType.FIELD
@Target(value = {ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation{
String name(); //default "大娃"; 指定默认值
int id(); //default 1; 指定默认值
}*/