|
From: Reuben G. <reu...@ay...> - 2005-08-08 03:23:09
|
Hey Guys,
I noticed in a couple of places in the code we are using
public void getObjectsOfType(Class c) {
if (o.getClass().equals(c))
add(o);
.
.
.}
When we want objects of a certain type, 90% of the time, we mean any
objects of a certain type and all of it's subtypes. So, for example,
if we have a list of Objects and we want to get all the Agents from
it, we want to get all the InfoAgents, SpecialAgents, BubbleAgents,
etc...
The correct code is the following:
public void getObjectsOfType(Class c) {
if (c.isAssignableFrom(o.getClass()))
add(o);
.
.
.}
someClass.isAssignableFrom(anotherClass) is like saying
someClass.isSuperClassOf(anotherclass)
-Reuben
--
Reuben Grinberg
reu...@ay...
(609)233-9891
"We'll be saying a big hello to all intelligent lifeforms
everywhere... and to everyone else out there, the secret is to bang
the rocks together, guys!"
-- The Hitchiker's Guide to the Galaxy
|