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 上的注解,其它都无法继承。

### 关于 `inherited` 的含义和用法 在面向对象编程(Object-Oriented Programming, OOP)的上下文中,关键字 `inherited` 主要用于表示子类从父类继承的内容。具体而言,当一个类派生自另一个类时,它可以自动获得父类的方法、属性和其他成员[^1]。然而,不同编程语言对这一概念的具体实现方式有所不同。 #### Delphi 和 Pascal 中的 `inherited` 在某些编程语言如 Delphi 或 Object Pascal 中,`inherited` 是一个显式的关键词,用来指代当前方法是从父类继承而来的版本。它的主要用途包括: - **调用父类方法**:如果子类重写了某个来自父类的方法,则可以通过 `inherited` 调用原始定义的方法。 ```pascal procedure TChildClass.MyMethod; begin inherited MyMethod; // 显式调用父类中的同名方法 writeln('Additional functionality in child class'); end; ``` - **访问父类字段或属性**:虽然不常见,但在特殊情况下也可以通过 `inherited` 来引用父类中声明的数据成员。 #### Python 和 Java 中隐含的继承机制 尽管像 Python 和 Java 这样的现代 OO 编程语言并没有专门的关键字叫做 `inherited`,它们仍然支持类似的继承行为。例如,在这些语言中,子类可以覆盖(override)或者扩展(extend)父类的功能,并且通常会使用 `super()` 函数来达到相同的效果。 - **Python 示例** ```python class Parent: def greet(self): print("Hello from parent") class Child(Parent): def greet(self): super().greet() # 类似于 Delphi/Pascal 的 'inherited' print("And hello from child too!") c = Child() c.greet() ``` - **Java 示例** ```java public class Parent { void displayMessage() { System.out.println("This message comes from the parent."); } } public class Child extends Parent { @Override void displayMessage() { super.displayMessage(); // 对应于 'inherited' 功能 System.out.println("Also displayed by the child."); } } ``` 上述例子展示了如何利用超类链路去执行基类的操作后再附加额外处理逻辑[^3]。 ### 总结 因此,“inherited”的核心意义在于强调一种关系——即某一部分代码来源于更高级别的原型或者说模板;而在实际应用层面则表现为允许开发者明确指定何时何地应该回退至祖先节点所提供的默认实现路径之上加以构建新的特性集合。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

会编程的吕洞宾

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

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

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

打赏作者

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

抵扣说明:

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

余额充值