java "generic generic type" -
in application, have board. board consists of cell. each cell has int value. there few types of boards extend board. each type of board represent cells differently. example, 1 use list , anothr use tree.
so tried:
public abstract class board<t> { t<cell> cells; //error - type t not generic; cannot parameterized arguments <cell> protected abstract void setvalues(t<integer> values); }
unfortunately, can't make t generic type. how implement that?
i found approach complicated. peter.petrov close do.
if list , tree (treeset) need, both implements interface collection why not use ?? need work beside of add, removing, iterating ?
so class (i added little bit java8 features)
public abstract class board { collection<cell> cells; // don't need 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 need (for reason extra, extend class , implement/override methods special behaviour). helped, don't see use case example.
Comments
Post a Comment