package com.bili;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
//ElementType.METHOD 作用在方法上
//RetentionPolicy.RUNTIME 注解的生命周期
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface VIP{
String level() default "";
}
class Dog{
@VIP(level = "3")
public void one(){
}
}
public class test {
public static void main(String[] args) throws NoSuchMethodException {
//获得这个类的字节码
Class<?> aClass = Dog.class;
//得到Dog类中one方法
Method one = aClass.getMethod("one");
//获得one中的注解
VIP vip = one.getAnnotation(VIP.class);
System.out.println(vip.level());
}
}