Tuesday 15 January 2013

java - How can I pass parameters from the outside to an implementation of an inteface -



java - How can I pass parameters from the outside to an implementation of an inteface -

i created generic info construction , implemented custom iterator, iterator receive interface has function, each object in info construction sent function interface on object.

here how that:

the interface:

public interface qtreeaction<t> { void acttodo(t param); }

the iterator:

public void iterate(qnode qnode, qtreeaction<t> action) { if (qnode == null) return; (int j = 0; j < qnode.objlist.size(); j++) { action.acttodo(qnode.objlist.get(j).object); } (int = 0; < 4; i++) { this.iterate(qnode.nodes.get(i), action); } }

and call:

public void render(float fdelta, canvas g) { g.drawrgb(255, 255, 10); g.setmatrix(this.gamematrix); ohhandler.render(new qtreeaction<tile>() { @override public void acttodo(tile param) { param.render(fdelta, g); } }); this.pplayer.render(fdelta, g); }

(ohhandler has node , calls iterator)

the render function:

public void render(float fdelta, canvas g) { g.drawbitmap(staticdataholder.gametextures.blocks[tttype.getvalue()], this.vectileposition.getx(), this.vectileposition.gety(), null); }

as see param.render requires 2 parameters provided function, @ situation there error , asks me create params final throws , error @ runtime.

how can overcome this?

in case can reference final variables within anonymous class.

this should trick:

public void render(float fdelta, canvas g) { final cavas gfinal = g; final float fdeltafinal = fdelta; ohhandler.render(new qtreeaction<tile>() { @override public void acttodo(tile param) { param.render(fdeltafinal , gfinal); } }); this.pplayer.render(fdelta, g); }

java android generics parameters interface

No comments:

Post a Comment