1,什么是注解的解析:
2,注解相关的接口及方法:
3,如何解析注解:(在谁的头上就用谁来解析)
- 如果注解在构造方法上,使用Constructor来获取;
- 如果注解在成员方法上,使用Method来获取;
-
4,注解解析的使用:
```java //注解: @Retention(RetentionPolicy.RUNTIME) //定义注解 public @interface Myin {
//注解属性; String name(); int age(); String[] auth();
}
//使用注解类: public class Set {
//使用注解并赋值:@Myin(name = "ddd",age = 19,auth = {"dd", "dad"})public void sale(){}
}
//main: public class Text {
public static void main(String[] args) throws NoSuchMethodException {//到set类的class对象Class<Set> setClass = Set.class;//利用class对象获取在使用注解类中的sale方法对象Method method = setClass.getMethod("sale");//判断解析的注解是否存在:boolean b = method.isAnnotationPresent(Myin.class);if (b){//利用sale方法对象获取一个注解:Myin annotation = method.getAnnotation(Myin.class);//输出注解对应的属性内容:System.out.println(annotation.name() +"\t"+ annotation.age() + Arrays.toString(annotation.auth()));}else {System.out.println("out");}}
} ```
