type conversion for user defined classes
I have a lot of classes which are basically equivalent, ie have almost the same functions in开发者_如何转开发 all respect. Let's call them A and B. There are hundreds of methods that are using A and now due to code changes I have to change them to B.
Is there a way by which I can program a mapping between A and B such that I have minimal code changes?
If A
is not final
, you can make B extends A
, and have B
's methods @Override
A
's. Then wherever previously you were invoking methods on an instanceof A
, you now provide an instanceof B
, and let dynamic dispatch handle the rest.
This is called polymorphism. It only works with non-static
methods having the same exact signature. You can not @Override
static
methods.
See also
- Java Tutorials/Object-Oriented Programming Concepts
- http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming
Related questions
- Why doesn’t Java allow overriding of static methods ?
On interfaces
Depending on why you were doing this, you should know learn the concept of interfaces and how they're used in object-oriented programming to allow precisely this kinds of flexibility and convenience.
Consider the interface List<E>
, for example, and an implementation ArrayList<E>
. If you wrote an entire library that works with an ArrayList<E>
, doing all the usual add/addAll/remove
etc, and now you must use a LinkedList<E>
instead, then you'd have little choice but to go to the source code and change all the ArrayList<E>
to LinkedList<E>
, and hope that the change doesn't break another code which still assumed that ArrayList<E>
was used.
If instead your library works with a List<E>
, then switching to a LinkedList<E>
need to be done only wherever the objects are created. All the other code that was doing the add/addAll/remove
would've still worked just fine, since those are methods that are defined in the interface List<E>
which all implementors will have.
It's not clear from the current context, but if A
and B
are so similar, then perhaps they belong to some type X
. If so, you should consider defining interface X
, and have A implements X
, and B implements X
.
See also
- Java Tutorials/Object-Oriented Programming Concepts/Interfaces
- Effective Java 2nd Edition, Item 52: Refer to objects by their interfaces
Related questions
- Is it just me or are interfaces overused?
- Why are interfaces preferred to abstract classes?
- Explaining Interfaces to Students
You should define an interface that both A and B implement.
Well, you should probably use inheritance in this case.
If you make B
a subclass of A
, you can use B
wherever you had been using A
without changing any of the code.
Sun/Oracle has some great tutorials on inheritance that you may want to take a look at in order to get started.
There are many ways, each with advantages and disadvantages. A couple ideas:
Make B a subtype of A. Then you can pass instances of B to methods requiring A. Alternatively, extract a common interface, and replace uses of A with that interface where possible (a good IDE can do this automatically).
In A, include the following constructor:
A(B b) {
// create an A with the data from b
}
or create an Adapter that subclasses A and delegates all calls to B.
In any case, if minimizing tedium in changing the code is your objective, look into your IDE's refactoring operations.
You can make A an adapter for B. I.e.
class A {
private B b = new B();
public String someMethod() {
return b.equivalentSomeMethod();
}
}
That's in case the methods are similar, and not exactly the same. If they are exactly the same - use your IDE to get rid of one of the classes and replace it with the other.
精彩评论