实例化泛型类并实现通用接口

时间:2013-12-21 08:08:59

标签: java generics inheritance

我有这堂课:

public DrawItem {
    protected String getSeperator() {
        return "";
    }
    .......
    // some other methods
}

我还有另一个扩展DrawItem的课程。

public DrawNumber extends DrawItem {
    @Override
    protected String getSeperator() {
        return "-";
    }
}

现在,在通用类CombinationGenerator<E>中,我正在尝试实例化DrawItem / DrawNumber的对象。由于在java中不能实例化泛型类型(如new E(...)),我根据this回答创建了一个Factory接口。

public interface DrawItemFactory<E> {
    E create(...);
}

然后在CombinationGenerator<E>班,

public class CombinationGenerator<E> {
    DrawItemFactory<E> factory;

    public CombinationGenerator<E>(DrawItemFactory<E> factory) {
        this.factory = factory;
    }

    public List<E> generate() {
        ......
        list.add(factory.create(...));
        ......
    }
}

现在DrawNumber类实现了DrawItemFactory<DrawItem>接口。

public DrawItem implements DrawItemFactory<DrawItem> {
    protected String getSeperator() {
        return "";
    }

    @Override
    public DrawItem create(...) {
        return new DrawItem(...);
    }
    .......
    // some other methods
}

我可以创建CombinationGenerator<DrawItem>课程。

DrawItem drawItem = new DrawItem(...);
CombinationGenerator<DrawItem> generator = new CombinationGenerator<DrawItem>(drawItem);
List<DrawItem> combinations = generator.generate();

到目前为止,一切都很好。但是当我尝试创建这样的DrawNumber类时,

public DrawNumber implements DrawItemFactory<DrawNumber> {
     ....
}

它给了我以下错误:

The interface DrawItemFactory cannot be implemented more than once with different arguments: DrawItemFactory<DrawItem> and DrawItemFactory<DrawNumber>

我尝试了this解决方案,但我遇到了同样的错误。还有其他办法吗?

2 个答案:

答案 0 :(得分:1)

而不是使用所有这些工厂,你可以做这样的事情:

public class CombinationGenerator<E> {

    E instance;

    public CombinationGenerator(Class<E> clazz) {
        Constructor<?> con = clazz.getConstructor();
        this.instance = (E) con.newInstance();
    }
}

...
CombinationGenerator<DrawNumber> cg = new CombinationGenerator<DrawNumber>(DrawNumber.class);

答案 1 :(得分:1)

根据@JB Nizet的评论,我通过创建两个单独的工厂类来解决这个问题:

public interface ItemFactory<E> {
    E create(int[] values);

    public static class DrawItemFactory implements ItemFactory<DrawItem> {

        @Override
        public DrawItem create(int[] values) {
            return new DrawItem(values);
        }           
    }

    public static class DrawNumberFactory implements ItemFactory<DrawNumber> {

        @Override
        public DrawNumber create(int[] values) {
            return new DrawNumber(values);
        }           
    }
}

CombinationGenerator

public class CombinationGenerator<E> {
    ItemFactory<E> factory;

    public CombinationGenerator<E>(ItemFactory<E> factory) {
        this.factory = factory;
    }

    public List<E> generate() {
        ......
        list.add(factory.create(...));
        ......
    }
}

并像这样实例化CombinationGenerator

DrawNumber drawNumber = new DrawNumber();
CombinationGenerator<DrawNumber> generator = new CombinationGenerator<DrawNumber>(new ItemFactory.DrawNumberFactory());

List<DrawNumber> combinations = generator.generate();