gpt4 book ai didi

java - Java中的继承是如何实现的?

转载 作者:行者123 更新时间:2023-12-01 17:20:20 25 4
gpt4 key购买 nike

Java 中继承到底是如何实现的?例如,考虑一下:

class A {
public void foo() {
System.out.print("A");
}
}
class B extends A {
...
}
class Test {
public static void main(String[] args) {
B test = new B();
test.foo(); // how is foo() called?
}

在该行下面,编译器是否会将 A.foo() 的定义转储到类 B 的主体中?喜欢

class B extends A {
...
public void foo() {
System.out.print("A");
}
}

或者 foo 是否以某种方式在 A 类中查找并在那里调用?

最佳答案

这可能会对您有所帮助,《Ivor Horton's Beginning Java 7》一书中的解释

I said at the beginning of this chapter that a derived class extends a base class. This is not just jargon — it really does do this. As I have said several times, inheritance is about what members of the base class are accessible in a derived class, not what members of the base class exist in a derived class object. An object of a subclass contains all the members of the original base class, plus any new members that you have defi ned in the derived class. This is illustrated in Figure 6-3.

enter image description here

关于java - Java中的继承是如何实现的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19241499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com