gpt4 book ai didi

java - 调用未覆盖方法的语法

转载 作者:行者123 更新时间:2023-11-29 06:19:57 24 4
gpt4 key购买 nike

我有

public class D extends B
{
public void method() {}
}

public class B
{
public void method() {}

public void anotherMethod() { method(); }
}

在上面,如果你持有 D 的一个实例,比如 d,d.anotherMethod() 会导致调用 D.method。

Java 中是否有从 anotherMethod() 内部调用 B.method() 的语法?

最佳答案

不,没有。派生类必须包含对 super.method() 的调用。

如果 B 想要防止子类覆盖 method(),它应该将 method() 声明为 final.

关于java - 调用未覆盖方法的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3533772/

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