gpt4 book ai didi

java - 如何将 super 赋值给变量?

转载 作者:太空宇宙 更新时间:2023-11-04 07:47:16 25 4
gpt4 key购买 nike

我想执行以下操作:

public class Sub extends Super {
public Sub(Super underlying) {
if (underlying == null) {
underlying = super; // this line is illegal
}

this.underlying = underlying;
}

@Override
public void method() {
underlying.method();
}
}

这样的事情怎么办?

最佳答案

你没有正确理解java中的super关键字。请参阅javadoc for super

If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super. You can also use super to refer to a hidden field (although hiding fields is discouraged)

此外,super() 用于调用父类构造函数。

关于java - 如何将 super 赋值给变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15094972/

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