gpt4 book ai didi

java - 是否可以显式访问嵌套类的包含类的实例成员?

转载 作者:行者123 更新时间:2023-11-30 04:50:02 24 4
gpt4 key购买 nike

java 中是否有关键字允许从嵌套类中显式调用包含实例(或其父类(super class))的成员?

场景

public class Superclass {
public void doSomething() {
// do something that's of interest to the superclass
}
}

public class Subclass extends Superclass {
@Override
public void doSomething() {
// do something that's of interest to the subclass
// possibly call super.doSomething()
}

private class NestedClass {
private void doSomething() {
// do something that's of interest to the nested class
// calling doSomething() here without an explicit scope
// specifier results in a stack overflow
}

private void doSomethingElse() {
if (somethingOfInterestToTheSubclassIsNotImportant) {
// just invoke the superclass's doSomething() method
} else {
// invoke the subclass's doSomething() method
}
}
}
}

最佳答案

哦,完全可以。只需使用 Subclass.this.doSomething()Subclass.super.doSomething() 即可获取父类(super class)方法。

关于java - 是否可以显式访问嵌套类的包含类的实例成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10034291/

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