Assuming I create a method which is passed an object and that method would perform an action depending on the object passed. How should I identify the object?
I thought of using the class name to identify the object, but that may be impractical since I could easily change the class name of objects, and generate headaches during future development. Am I right?
edit: for example, i have objects ball and bomb. if i have a开发者_开发问答nother object called wall, and the wall has the method to resolve collisions with the wall (e.g. the coordinates of the colliding ball and bomb) but have different logic depending on the colliding object (i.e. ball and bomb)
What you are asking for is the instanceof
operator.
if (object instanceof SomeClass) {
// do something
} else if (object instanceof SomeOtherClass) {
// do something else
}
However, this is not a good practice. Instead you may use the so called double-dispatch. Make the object that is passed conform to an interface which defines the operation in terms of the other class. So:
public interface ThrowableItem {
void throwAt(Wall wall);
}
public class Wall {
void accept(ThrowableItem item) {
item.throwAt(this);
}
}
And then provide the appropriate implementations within Ball
and Bomb
(both of which implement ThrowableItem
)
Take a look at the Visitor pattern - you can move the operations to a WallVisitor
which knows how to handle the colisions for each object.
this code will lead you to using switch
consider using polymorphism. and this will cancel naming problem automatically.
It depends on what the actions are. How are they related? Is it that you have a number of objects that do the same thing but in slightly different ways? For example, suppose I have a method that is supposed to print a document but I want the same method to print pdf and doc files.
If your situation is similar to that then you may want to consider using inheritance as follows: Create a super class, in my example lets call it Document with a print() method. The print method does not have to do anything. Then create a subclass for each type of document, so I will end up with a PdfDocument and a DocDocument subclass. Each of these will provide an implementation for print() that can print the type of document that it relates to.
Then the method that I write will be:
public void printDocument(Document d){
d.print();
}
That is, by targetting the super class type I do not have to worry about the specific action that each type of document does. That way I avoid code that checks the type of object that is being passed to my method. It makes the code more robust for future extension.
Depending on the context, you might also want to provide different methods (overloading) for the different possible types that may be passed in.
public void doSomething(TypeA object) {
// TypeA specific stuff
}
public void doSomething(TypeB object) {
// TypeB specific stuff
}
You can use instanceof
but beware of the pitfalls. It will be 'true` for subclasses as below :
if (subclass instanceof superclss) // returns true
精彩评论