简体   繁体   English

java“泛型通用类型”

[英]java “generic generic type”

In my application, I have a Board. 在我的申请中,我有一个董事会。 A Board consists of cell. 董事会由单元组成。 Each cell has an int value. 每个单元格都有一个int值。 There are few types of Boards that will extend Board. 扩展董事会的董事会类型很少。 Each type of Board will represent the cells differently. 每种类型的董事会将代表不同的单元。 For example, one would use a List and anothr will use a Tree. 例如,一个将使用列表,另一个将使用树。

So this is what is tried: 所以这是尝试的方法:

public abstract class Board<T> {
    T<Cell> cells;  //error - The type T is not generic; it cannot be parameterized with arguments <Cell>

    protected abstract void setValues(T<Integer> values);
}

Unfortunately, I can't make T a generic type. 不幸的是,我不能使T为泛型类型。 How do I implement that? 我该如何实施?

I found your approach too complicated. 我发现您的方法太复杂了。 Peter.petrov is close to what I will do. Peter.petrov接近我要做的事情。

If List and Tree (TreeSet) is what you need, both implements interface Collection so why not use this ?? 如果您需要列表和树(TreeSet),则两者都实现接口Collection,所以为什么不使用它呢? Do you need anything extra to work with beside of add, removing, iterating ? 除了添加,删除,迭代之外,您是否还需要其他任何工作?

Collection (Javadoc) 集合(Javadoc)

So you class could look (I added little bit Java8 features) 这样您就可以上课了(我添加了一点Java8功能)

public abstract class Board {
    Collection<Cell> cells;  

    // Don't need to be abstract
    protected void setValues(Collection<Integer> values) {
        Stream<Cell> transformed = values.stream().map(Cell::new);
        cells.addAll(transformed.collect(Collectors.toList()));
    }
}

class Cell {
    private Integer value;

    Cell(Integer value) {
        this.value = value;
    }
}

If you need (for some reason something extra, extend this class and implement/override methods that will do special behaviour). 如果需要(出于某些原因,请扩展此类并实现/重写将具有特殊行为的方法)。 Hopefully I helped, but I really don't see a use case for your example. 希望我能提供帮助,但我确实看不到您的示例的用例。

I don't really see much of a need to use generics here. 我真的没有在这里使用泛型的必要。

public abstract class Board {
    Holder<Cell> cells;   

    protected abstract void setValues(Integer[] values);
}

Now have your List and Tree implement/extend Holder. 现在有了您的列表和树实现/扩展持有人。
You can have 2 classes ListHolder and TreeHolder which both hold 您可以有两个类ListHolder和TreeHolder都持有
cells but one is backed by a List, the other one by a Tree. 单元格,但一个由列表支持,另一个由树支持。

Actually maybe it makes more sense the setValues to be a 实际上,将setValues设置为a更有意义
method on Holder which both ListHolder and TreeHolder implement. ListHolder和TreeHolder都实现的Holder上的方法。

You would need to have something like: 您将需要具有以下内容:

public abstract class Board<T extends OtherGenericClass<TT>, TT> {
    T cells;
    public Board(T cells) {
        this.cells = cells;
    }
    protected abstract void setValues(T<Integer> values);

    public static void main(String[] args) {
        Cell<Integer> cells = new Cell<Integer>();
        Board<Cell<Integer>,Integer> board = new Board<Cell<Integer>,Integer>(cells);
    }
}

This does not seem like what you want though...I think you would rather have this: 尽管这似乎并不像您想要的...我想您宁愿这样:

public abstract class Board<T> {
    Cell<T>[] cells;
    public Board(Cell<T>[] cells) {
        this.cells = cells;
    }
}

with a Cell class: Cell类:

public abstract class Cell<T> {
    T cellValue;
    public Cell(T cellValue) {
        return cellValue;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM