java - "super"在继承方法中引用哪个类?

标签 java inheritance method-call

我在这里问这个问题是因为我真的不知道应该如何谷歌这个。我需要确认一些我今天可能想到的事情。几天前,我认为如果一个对象继承了其父亲的方法,则意味着它基本上在其代码中“拥有”该方法。但后来我问如果我们这样做会怎样:

class B extends A {

  public B() {
  }

  public int getValue() {
     return 2 * super.getValue();
  }

}

class C extends B {

  public C() {
  }

  public int getValue(int b) {
     return 5 * b;
  }
}

假设有一个类 A 也具有 getValue() 方法。现在,类 C 的对象使用它从 B 继承的“其”getValue() 方法(不带参数)。

但是 super.getValue() 仍然引用类 A,即使调用它的方法是继承的。

继承并不意味着类 C 在其代码中“拥有”该方法,而是意味着它可以使用 B 中的方法,对吗? super() 仍然引用其“原始”类的父类(super class)。

这个见解正确吗?

最佳答案

Inheritance does not mean that class C "has" the method in its code, but that it can use it from B, right?

是的。

super() still refers to the superclass of its "original" class.

是的。你是对的。 super 始终指的是使用它的父类。在本例中,它在 B 中使用,它的父级是 A。所以引用那里。

关于java - "super"在继承方法中引用哪个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25506173/

相关文章:

java - 按执行顺序列出给定进程的所有任务

jquery - CSS继承问题。请帮助CSS。

c++ - COM-扩展“接口(interface)”?

c++ - 覆盖虚函数的问题

javascript - UIWebView滚动时调用方法

java - 有没有Java概率库?

java - 从 View 条目获取 Notes 日期时间到 JAVA 日期

java - 从ListQueue类调用 protected 方法 "Front",该方法实现了Queue接口(interface)

java - 如何在 ASM 中获取类的 ByteArray

java - 如何在 Spring 和 EJB 中使用相同的事务?