Java: Calling a method in a raw Class which's name is undefined? -
right, first have no idea called hope title correct?
in main class main.java it'll run piece of code:
classtwo ctwo new classtwo(); ctwo.addevent(new eventone()); ctwo.addevent(new eventtwo()); ctwo.addevent(new eventthree()); now within classtwo's addevent() have as:
public void addevent(class<?> event) { event.update(); } the class<?> event put there have no idea else start at. event.update() of course comes error there no such method in type class. every event class have update() method in them how go making work?
edit: ok question wasn't clear. in classtwo made api , contains gameloop thread. interfaces have no idea java.lang.reflect.method looks want i'm looking for.
edit2: update method:
private bufferedimage gameimage; public void update() { graphics2d g = (graphics2d) gameimage.getgraphics(); g.setcolor(color.yellow); g.fillrect(0, 0, gsystem.getwidth() - 1, gsystem.getheight() - 1); /////////// // change image made here differ each event. /////////// ctwo.setgameimage(gameimage); }
one way solve have eventone, eventtwo , eventthree extend same interface (let's call event).
this event interface has 1 unimplemented method called update.
then addevent method this:
public void addevent(event event) { event.update(); }
Comments
Post a Comment