Friday 15 February 2013

java - Comparing custom object within a method -



java - Comparing custom object within a method -

in app (which android game - so, java), have custom class called quad utilize games objects. class creates textured opengl quad.

i have class called enemy extends quad.

i have method called game class , can pass in various objects. now, want different things depending on object passed in, i'll seek demonstrate code.

bird = new enemy(); //create bird sprite snail = new enemy(); //create snail sprite public void dosomething(quad sprite){ //do work here regardless of object passed in move(object); if (sprite == bird){ //do bird specific stuff here } else if {sprite == snail}{ //do snail stuff here } }

so, can see, want execute mutual code regardless of object passed method, (whether bird, snail or other object), after mutual code, there should object specific code run.

now, remember reading on 1 of other questions although worked, wasn't right thing do.

since ratio of code heavily skewed in favour of mutual code, (ie, bulk of code in method should run regardless of object is), doesn't seem great thought create different methods birds , snails. much code duplication.

i this:

public void dosomething(quad sprite){ move(object); } public void dobirdstuff(){ dosomething(bird); //bird specific code here } public void dosnailstuff(){ dosomething(snail); //snail specific code here }

and phone call specific object, so:

dosnailstuff(snail); dobirdstuff(bird);

however, seems overly complicated , inefficient

so question is, if comparing custom objects in way isn't 'java' thing do, how can accomplish goal in more acceptable way , why first method deemed unacceptable?

you can create 2 classes (bird , snail) extends quad , utilize instanceof :

public void dosomething(quad sprite){ //do work here regardless of object passed in move(object); if(sprite instanceof bird) { //do bird specific stuff here } else if {sprite instanceof snail}{ //do snail stuff here } }

an illustration of how utilize :

public void main(){ bird bird = new bird(); snail snail = new snail(); // bird dosomething(bird); // snail dosomething(snail); } update

because code isn't specific bird/snail best way utilize enum define enemy type :

public enum enemytype{ bird, snail }

and utilize in enemy class :

public class enemy extends quad{ private enemytype mtype; //all other class members... // constructor type public enemy(enemytype type){ this.mtype = type; } public void doenemystuff(){ if(isbird()){ // bird stuff } else if(issnail()){ // snail stuff } } //check if current enemy bird public boolean isbird(){ homecoming mtype == enemytype.bird; } //check if current enemy snail public boolean issnail(){ homecoming mtype == enemytype.snail; } }

and wherever want can utilize method :

public void dosomething(quad sprite){ //do work here regardless of object passed in move(object); if(sprite instanceof enemy) { //do enemy specific stuff here ((enemy) sprite).doenemystuff(); } }

with first thought creating objects , keeping references them create check. shouldn't work because default behavior of "==" check equality of references (there is little explanation == operator).

java object if-statement comparison

No comments:

Post a Comment