java - Generic class initialization -
this class:
public class foodset<t extends concretefood>{ public list<t> food = new arraylist<t>(); public foodset() { /* * foodtype enum containing food types e.g: rice, pork, beef ... */ for(foodtype foodtype : foodtype.values()) { /* * 1(*) * / food.add( (t)new concretefood(foodtype,0) ) } } } and in 1(*) problem, how init list, 't' type ? init concretefood, giving argument foodtype , amount, goal init list t extends concretefood. every subclass of concretefood has access foodtype , food count. init list apropriate concretefood subclass , init each concretefood object foodtype , count = 0. how should ?
this isn't want. need factory class create instances of concretefood you.
public class foodfactory { private static final foodfactory instance = new foodfactory(); private foodfactory() {} public static foodfactory getinstance() { return instance; } public food create(foodtype type) { // put type checks here } } i don't care naming much. "concretefood"? doesn't sound appetizing. why not foodcategory?
Comments
Post a Comment