Inherited元注解使用

@Inherited元注解使用

❓我们都知道java的三大特性:封装、继承、多态。本次主要谈谈跟继承有关系的内容,继承父类,可以在子类中调用父类的一些方法,那么在使用自定义注解的时候是否也可以继承在父类上使用的注解内容呢?

接下来我们来上手实战,自定义一个注解。

自定义注解(Value)
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Value {

    String value();
}

Parent类

@Value(value = "parent")
public class Parent {
}

Child类

public class Child extends Parent {
}

测试类

import java.lang.annotation.Annotation;

public class AnnoMain {

    public static void main(String[] args) {
        Annotation[] annotations = Child.class.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation.annotationType() + " " + annotation);
        }
    }
}
没有使用Inherited情况下输出
  • 执行结果:

在这里插入图片描述

使用Inherited情况下输出
  • 调整下Value注解
import java.lang.annotation.*;

@Inherited // 添加Inherited
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Value {

    String value();
}
  • 执行结果:

在这里插入图片描述

那么如果我在子类上添加一个Value注解,输出的内容又会是什么样子呢?

  • 调整Child类,添加注解Value
  @Value(value = "child")
  public class Child extends Parent {
  }
  • 执行结果:

在这里插入图片描述

我们会发现,此处输出的结果是child,在子类与父类同时添加自定义注解时,子类会覆盖掉父类的注解内容。

💡这时我就有个新的疑问了,既然继承可以如此使用注解,那么如果是接口类的实现呢?话不多说,直接开写代码。

编写IParent接口类和ChildImpl实现类

IParent接口类

@Value(value = "iparent")
public interface IParent {
}

ChildImpl实现类

public class ChildImpl implements IParent {
}

测试类

import java.lang.annotation.Annotation;

public class AnnoInterfaceMain {

    public static void main(String[] args) {
        Annotation[] annotations = ChildImpl.class.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation.annotationType() + " " + annotation);
        }
    }
}

执行结果:

在这里插入图片描述

可以看出,输出的内容为空,接口类添加注解无法输出注解内容。

那么我们再做一次调整,在实现类上添加注解Value

调整ChildImpl实现类

@Value(value = "ichild")
public class ChildImpl implements IParent {
}

执行结果:
在这里插入图片描述

❗️通过实现可以看出接口上注解是无法解析出结果的,只有类上可以解析成功,换而言之就是说只能继承 class 上的注解,其它都无法继承。

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
`@Inherited` 是一个标准的 Java 注解(meta-annotation),用于指示一个注解是否可以被继承。当一个注解被标注为 `@Inherited` 后,它将可以被子类继承。 具体来说,当一个被 `@Inherited` 标注的注解被放置在一个父类上时,它将会被子类继承,并且子类上也会具有该注解。这意味着,如果我们在父类上使用了一个被 `@Inherited` 标注的注解,那么所有继承该父类的子类也将自动具有该注解。 需要注意的是,`@Inherited` 注解仅对类级别的注解有效,对方法、字段等其他素的注解无效。 下面是一个示例,展示了如何使用 `@Inherited` 注解: ```java import java.lang.annotation.*; @Inherited @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface MyAnnotation { // 注解素 String value(); } ``` 在上面的示例中,我们定义了一个自定义注解 `@MyAnnotation` 并标注了 `@Inherited` 注解。当我们将 `@MyAnnotation` 注解放置在一个父类上时,该注解将会被子类继承。 ```java @MyAnnotation("Parent") public class ParentClass { // 父类的代码内容 } public class ChildClass extends ParentClass { // 子类的代码内容 } ``` 在上面的示例中,`ChildClass` 继承自 `ParentClass`,由于 `@MyAnnotation` 使用了 `@Inherited` 注解,所以 `ChildClass` 也会自动具有 `@MyAnnotation("Parent")` 注解。 总结一下,`@Inherited` 是一个注解,用于指示一个注解是否可以被继承。当一个注解被标注为 `@Inherited` 后,它将可以被子类继承。但需要注意,它仅对类级别的注解有效,对其他素的注解无效。 希望能够解答你的疑问。如果还有其他问题,请随时提问。谢谢!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风之殇2016

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值