简体   繁体   中英

java “generic generic type”

In my application, I have a Board. A Board consists of cell. Each cell has an int value. 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. How do I implement that?

I found your approach too complicated. Peter.petrov is close to what I will do.

If List and Tree (TreeSet) is what you need, both implements interface Collection so why not use this ?? Do you need anything extra to work with beside of add, removing, iterating ?

Collection (Javadoc)

So you class could look (I added little bit Java8 features)

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
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
method on Holder which both ListHolder and TreeHolder implement.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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