gpt4 book ai didi

java - 访问抽象类方法

转载 作者:行者123 更新时间:2023-11-30 01:55:22 25 4
gpt4 key购买 nike

我有三个不同的类(class):

1-)

abstract class A {
abstract void one();
void two(){
System.out.println("two");
one();
}
abstract void three();
}

2-)

abstract class B extends A {
void one() {
System.out.println("one");
three();//I think this method has to run
}

void three() {
System.out.println("3");//That
}
}

3-)

public class C extends B {
void three(){
System.out.println("three");
}

}

在Main方法中

public static void main(String [] args){
C c=new C();
c.one();
c.two();
c.three();
}

输出:

one
three
two
one
three
three

但我认为在第二个代码中,one() 方法必须运行其三个方法,并且必须显示“3”而不是“三”,但此代码在 C 类中运行三个。

最佳答案

在 B 类和 C 类中都重写了 Three() 方法

由于 c 是 C 类的实例,因此任何对 c 对象的 Three() 方法的引用都将调用 C 类中的 Three() 实现

关于java - 访问抽象类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54714988/

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