Results 1 to 4 of 4
Thread: return subtype
- 03-18-2011, 07:18 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
return subtype
Think I got the title right there.
Basically I have a superclass called SpaceObject. I have another superclass below that of SpaceShip(and then different spaceships below this)
I have an arraylist of SpaceObjects. How can I know if an element in my arraylist of SpaceObjects is a SpaceShip?
public SpaceObject getType(){
return this;
}
I imagine something like this would work, but can't work out how I would do an if statement to check if its type SpaceShip (and what type of spaceship would be handy)
Thanks!
- 03-18-2011, 07:27 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
If I understand correctly, instanceof might be what you are looking for
Note that calling something likeJava Code:if ( this instanceof SpaceShip ){ ///I'm a spaceship }
will evaluate to true for all child classes as well. There are other ways to go about this, for example defining the type in the constructors.Java Code:if ( myClass instanceof SpaceObject){//true for all child classes of SpaceObject }
- 03-18-2011, 07:35 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
Yup
That's done the trick, thx alot
- 03-18-2011, 10:48 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
return array
By rfviki in forum New To JavaReplies: 2Last Post: 11-03-2010, 01:05 PM -
If Statement return
By ricardo5222 in forum New To JavaReplies: 4Last Post: 11-01-2010, 11:43 AM -
Using int/int, 7/5 would return 1
By zoe in forum New To JavaReplies: 2Last Post: 12-02-2008, 11:25 AM -
about 'return'.
By helloworld in forum New To JavaReplies: 9Last Post: 11-28-2008, 04:08 AM -
if..else..return
By mqdias in forum New To JavaReplies: 1Last Post: 08-10-2007, 04:20 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks