This site requires JavaScript, please enable it in your browser!
Greenfoot back
Entity1037
Entity1037 wrote ...

2013/10/21

Check to see if an actor that is referenced as its parent class is one of the parent class' subclasses?

Entity1037 Entity1037

2013/10/21

#
I've referenced a Terrain.class from a List, and I need to check if it's actually a subclass of Terrain. Is there any way to do that, or do I have to check for every individual class?
bourne bourne

2013/10/21

#
EDIT: You may use someObject.getClass().getSuperclass() and check if that's Terrain. I guess that may only work for single level difference in inheritance from Terrain.
danpost danpost

2013/10/21

#
You can use 'if (someObject instanceof SubclassName)'; but you will have to check each subclass (unless you know what subclass you are looking for). If you add a 'id' field in the Terrain class and set it differently in each of the subclasses, you can then reference it to determine what class it is.
Entity1037 Entity1037

2013/10/21

#
"if (someObject instanceof SubclassName);" should be sufficient. I'm just seeing if the object is a certain class, and if it is, then get certain information and save it in a specific format as a String for an algorithm.
You need to login to post a reply.