java - Create general method to fit interface and general parent class -
i have following method:
private void setfilledandadd(shape obj, color col, int x, int y) { obj.setfilled(true); // needs interface fillable obj.setfillcolor(col); add(obj, x, y); // needs children of shape (or shape itself) } if add 1 of lines:
setfilledandadd(oval, color, x, y); compile time error apears in line obj.setfilled(true); , lineobj.setfillcolor(col);. because shape not fillable. undefined type shape.
changing argument type in method setfilledandadd fillable (not shape) leads compile time error in line add(obj, x, y);. needs shape in case.
children of shape use fillable. give me hint, how method working.
thanks.
you can use generics expect object has both characteristics
private <t extends shape & fillable> void setfilledandadd(t obj, color color, int x, int y){ obj.setfilled(true); // needs interface fillable obj.setfillcolor(color); add(obj, x, y); } private void add(shape s, int x, int y){ // whatever code have goes here. } this compiles fine me.
Comments
Post a Comment