我的工厂出了什么问题?
我有一些像这样的代码:
public abstract class Foo {
public static Foo getFoo() {
return new FooImpl();
}
abstract void DoFoo();
private class FooImpl extends Foo {
public FooImpl() { }
@Override
void DoFoo() { }
}
}
但是 Eclipse 告诉我 没有可以访问类型 Foo 的封闭实例。
那么我怎样才能让它工作呢?
我试图使其尽可能简单,看看它是否可以编译:
public abstract class Foo {
public static Foo getFoo() {
return new FooImpl();
}
private static class FooImpl extends Foo {
public FooImpl() { }
}
}
但我仍然遇到相同的错误。我缺少什么?
固定的!我将行 return new FooImpl();
更改为 return new Foo.FooImpl();
I've got some code like this:
public abstract class Foo {
public static Foo getFoo() {
return new FooImpl();
}
abstract void DoFoo();
private class FooImpl extends Foo {
public FooImpl() { }
@Override
void DoFoo() { }
}
}
But Eclipse is telling me No enclosing instance of type Foo is accessible.
So how can I get this to work?
I attempted to make it as simple as possible to see if it would compile:
public abstract class Foo {
public static Foo getFoo() {
return new FooImpl();
}
private static class FooImpl extends Foo {
public FooImpl() { }
}
}
And I still get the same error. What am I missing?
FIXED! I changed the line return new FooImpl();
to return new Foo.FooImpl();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
很好的解释这里 - 简而言之,您需要创建类
FooImpl< /code>
static
,因此它仅绑定到外部类,而不绑定到外部类的特定实例(您没有)。getFoo
方法看起来也应该是静态的,顺便说一句——否则,您打算在Foo
的哪个实例上调用它?Excellent explanation here -- in brief, you need to make class
FooImpl
static
, so it's only tied to the outer class, not to a specific instance of the outer class (which you don't have). ThegetFoo
method also looks like it should be static, btw -- otherwise, what instance ofFoo
were you planning on calling it on?您希望人们如何调用
getFoo()
?除非您正在做一些完全时髦和激进的事情,否则您需要将其设为
静态
。How do you intend people to call
getFoo()
?Unless you're doing something completely funky and radical, you'll need to make it
static
.将
FooImpl
类设置为static
即可工作。Make the
FooImpl
classstatic
and it will work.