【问题标题】:Java: Creating an Instance of a Subclass from the Superclass in a static methodJava:以静态方法从超类创建子类的实例
【发布时间】:2012-11-11 21:20:59
【问题描述】:

我正在尝试从我的超类创建一个子类的新实例。这是我的超级班

public abstract class Worker {

    String world;

    protected abstract void onLoad(Scanner read);

    public static Worker load(Scanner read) {
        // I want to create the instance of my sub class here and call it w
        w.onLoad(read);
        return w;
    } 

    public void setWorld(String world) {
        this.world = world;
    }

}

这是我的子类

public class Factory extends Worker {

    @Override
    protected onLoad(Scanner read) {
        setWorld(read.readline());
    }

}

这就是我想要对这些类做的事情。

public class MainClass{

    public List<Factory> loadFactories() {
        List<Factory> facts = new ArrayList<Factory>();
        Scanner read = new Scanner(new FileInputStream("factory.txt"));

        while(read.hasNextLine()) {
            Factory f = (Factory)Factory.load(read);
            facts.add(f);
        }

        read.close();
        return facts;
    }

}

有什么方法可以做到这一点而无需重新开始?感谢您的帮助。

【问题讨论】:

  • 您还没有将Worker 设为子类。
  • 如果您希望 Factory 成为 Worker 的子类,一个好的开始是这样写:class Factory extends Worker
  • 这里应该是什么的子类?
  • 对不起,我写得太快了。
  • @user1816686 很难理解您要达到的目标。 Factory.load(read) 实际上调用了Worker.load(read),它返回了一个NoInputWorker,你将它分配给了一个Factory...一些拼图仍然丢失...

标签: java inheritance static-methods


【解决方案1】:

这是你想要的吗?

public static Worker load(Scanner read) {
    Factory w=new Factory();
    w.onLoad(read);
    return w;
} 

编辑:

public class MainClass {

    public List<Factory> loadFactories() throws FileNotFoundException, InstantiationException, IllegalAccessException {
        final List<Factory> facts = new ArrayList<Factory>();
        final Scanner read = new Scanner(new FileInputStream("factory.txt"));

        while (read.hasNextLine()) {
            final Factory f = Worker.load(read, Factory.class);
            facts.add(f);
            final Pipeline p = Worker.load(read, Pipeline.class);
        }

        read.close();
        return facts;
    }

    static public class Factory extends Worker {

        @Override
        protected void onLoad(final Scanner read) {

        }

    }

    static public class Pipeline extends Worker {

        @Override
        protected void onLoad(final Scanner read) {

        }

    }

    static public abstract class Worker {

        String world;

        protected abstract void onLoad(Scanner read);

        public static <T extends Worker> T load(final Scanner read, final Class<T> t) throws InstantiationException, IllegalAccessException {
            final T w = t.newInstance();
            w.onLoad(read);
            return w;
        }

        public void setWorld(final String world) {
            this.world = world;
        }

    }
}

【讨论】:

  • 不知道为什么这被否决了——如果你从表面上看问题,这是唯一的答案!
  • 对我来说,它看起来像是工厂设计模式的完美示例!我不可能用另一种方式来解释它。当然main方法应该是Worker f = Worker.load(read);
  • 是的,但不完全是。假设我有另一个名为 Pipeline 的 Worker 类,我想知道它是 Pipeline 还是 Factory(没有枚举)。
  • 就像 if (w instanceof Pipeline)... ?
  • 工厂 f = (Factory)Worker.load(read);可能会导致问题 - 如果您添加 Pipeline 类。一个典型的用途是返回 Worker,而不让用户知道你实际传回的类(允许你以后更改它)
猜你喜欢
  • 2021-05-14
  • 1970-01-01
  • 2011-06-07
  • 2019-04-15
  • 1970-01-01
  • 2012-12-19
  • 1970-01-01
  • 2012-07-04
相关资源
最近更新 更多