java注解接口Annotation

本文详细介绍了Java注解的工作原理,包括所有Java注解类型的基类接口Annotation,以及如何通过反射机制获取注解的属性值。通过具体示例,展示了如何在运行时获取类上的注解并读取其属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.介绍

所有的java注解类型都继承接口Annotation

2.获取注解的属性值

通过反射获取注解的属性值

@Retention(RetentionPolicy.RUNTIME)
public @interface Vui {
    String name() default "";
    boolean isUniuqe() default false;
}
@Vui(name = "zhang",isUniuqe = true)
public class TestAnn {

    public String getName() {
        System.out.println("hh");
        return "";
    }

    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
        TestAnn testAnnotation  = new TestAnn();
        Annotation[] annotations = testAnnotation.getClass().getAnnotations();
        for (Annotation annotation:annotations) {
            Method[] methods = annotation.annotationType().getMethods();
            for (Method method:methods) {
                System.out.println(method.invoke(annotation));
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值