我有一个抽象的包装类Foo,通过提供接口Reader来定义其功能。当我实现一个单独的Reader并提供它时,一切都很顺利。
但当我尝试使用内部类来完成这个操作时,就出了问题。对于我来说,在内部类中实现Reader是必需的。
public abstract class Foo
{
private Reader reader;
public Foo(Reader reader)
{
this.reader = reader;
}
public void read()
{
this.reader.doit();
}
}
由于一些中间构造函数调用,MapLink类型的封闭实例不可用。
public class ReaderFoo extends Foo
{
public class FooReader implements Reader
{
@Override
public void doit()
{
// functionality
}
}
public ReaderFoo ()
{
super(new FooReader());
}
}
我做错了什么?
new MapLink.MapReader()
隐式转换为new MapLink.MapReader(MapLink.this)
(如果MapReader
是内部类),而此时MapLink.this
尚不可用。 - Simon NickersonMapLink
和MapReader
的代码吗? - Matthias Meidsuper(new ReaderFoo().new FooReader());
用于非静态嵌套类。 正确 意味着在此处 编译不会出错,我更建议您使用静态类。 - Matthias Meid