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

2017/12/8

How do i add more than one class that can be added into a list?

Bonobo Bonobo

2017/12/8

#
The question is a bit hard to understand. Im not sure how to explain in properly. Im new to all of this. I want to get the objects in the range of 150 added in a list. It all worked for one class. (läufer.class). But how do i add another class. just hor helping you to understand: ...getObjectsInRange(150, läufer.class & läufer2.class) I know that wont work tho. Any ideas?
List<läufer>gegner = getObjectsInRange(150, läufer.class);
Super_Hippo Super_Hippo

2017/12/8

#
You could have a list for both and then add one to the other and you will have one list with both. The class names itself suggest that you don't really need to have two separate classes... or they could extend one superclass and you can use the superclass as the parameter.
danpost danpost

2017/12/8

#
List<? extends Actor> gegner = getObjectsInRange(150, läufer.class);
gegner.addAll(getObjectsInRange(150, läufer2.class));
The above is not tested -- but, I believe it should work. Be advised that the elements are typed as Actor objects and, as such, the methods or fields that are declared in either of the two classes are not accessible without first casting any of the objects appropriately as the type they actually are (as a läufer or läufer2 type object; you can use the 'instanceof' conditional to check which class the object belongs to).
You need to login to post a reply.