如何使外部类继承自内部类? [英] How to make an outer class inherited from an inner class?
问题描述
我怎样才能做出类似的工作:
How can I make something like this work:
class Outer {
int some_member;
abstract class InnerBase {
abstract void method();
}
}
class OuterExtendsInner extends Outer.InnerBase {
OuterExtendsInner(Outer o) { o.super(); }
void method() {
// How do I use some_member here?
// Writing Outer.this.some_member -> error about Outer not being an enclosing class
// Writing just some_member -> no-go, either
}
}
解决方法是拥有一个在InnerBase中返回Outer.this并从派生类调用THAT的方法,但还有另一种方法吗?
The workaround is to have a method in InnerBase that returns Outer.this and call THAT from derived classes, but is there another way?
我主要想从外面扩展InnerBase以便更好代码组织,但我可以将所有派生类移动到外部。
I primarily want to extend the InnerBase from outside in order to have better code-organization, but I could move all derived classes into Outer.
推荐答案
这里的问题是链接的合成字段 InnerBase
到外部
是私有字段。因此,我们只能从 InnerBase
中访问外部对象,或者如果某个方法或字段提供对同一对象的引用。
The problem here is that the synthetic field which links InnerBase
to Outer
is a private field. Thus, we can only access the outer object from within InnerBase
, or if some method or field there provides a reference to the same object.
这篇关于如何使外部类继承自内部类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!