Java – what’s wrong with my factory?

I have 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 told me that no instance of the enclosed foo type is accessible How can I make it work?

I try to make it as simple as possible to see if it will compile:

public abstract class Foo {
    public static Foo getFoo() {
        return new FooImpl();
    }

    private static class FooImpl extends Foo {
        public FooImpl() { }
    }
}

I still get the same mistake What did I miss?

Fixed! I changed the line and returned new fooimpl(); Return to the new foo FooImpl();

Solution

Excellent explanation here – in short, you need to make class fooimpl static, so it only binds to external classes, not specific instances of external classes (you don't) The getfoo method should also look static, by the way - otherwise, when are you going to call foo?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>