Polymorphism can be divided into variable polymorphism, method polymorphism, and class polymorphism. Here I emphasize the class polymorphism, which is often used in future work.
First of all, there is such a system: a student graduated from Dana and did a good job. He bought a Jetta Car. How should I design this system?
According to the idea of OO, We will abstract a class to represent Jetta automobile, which has a run () method.
Public class JD {
Public void run (){
System. Out. println ("JD is running at a 120-step speed ");
}
}
We will also abstract a class to represent the Representative. There is a drive () method which requires a car type as the parameter. First we will pass in a parameter of the JD type.
// This is our core business class
Public class person {
Public void drive (JD ){
JD. Run ();
}
Public static void main (string ARGs []) {
Person P = new person ();
JD = new JD ();
P. Drive (JD );
}
}
If you writeCodeCongratulations! You won the grand prize! --------------------- You Will Be Hacked by the Project Manager !!!!!!!
Why did the project manager cut you down?
Because the code you write is too frequent!
If our requirements change, this student will be more wealthy and buy one or two Benz students. What should we do in our previous system? Don't expect your system to never change
Our system can only be modified! This is why the project manager cut you down.
Our system will add a Benz class and a run () method.
Public class Benz {
Public void run (){
System. Out. println ("Benz is running at a speed of 200 ");
}
}
Our core business class also needs to be modified
Public class person {
/*
Public void drive (JD ){
JD. Run ();
}
*/
Public void drive (Benz B ){
B. Run ();
}
Public static void main (string ARGs []) {
Person P = new person ();
Benz B = new Benz ();
P. Drive (B );
}
}
In the future, we can abstract the car:
Public abstract class driver {
/* Attribute */
Public void run (); // Let the subclass run
}
Public Benz extends driver {
Public void run (){
System. Out. println ("Benz is running at a speed of 200 ");
}
}
Public JD extends driver {
Public void run (){
System. Out. println ("JD is running ...");
}
}
Public class person {
Private driver;
Public Person (){
}
Public Person (driver Driver ){
This. Driver = driver;
}
Public void drive (){
Driver. Run ();
}
Public void setdriver (driver Driver) {// using parameter polymorphism, you can buy any car in the future
This. Driver = driver;
}
Public static void main (string ARGs []) {
Person P = new person ();
JD = new JD (); // buy a JD if you have no money at the beginning
P. setdriver (JD );
P. Driver ();
Benz = new Benz {(); // you have money to change your car.
P. setdriver (Benz );
P. Driver ();
}
}
What is polymorphism?
To put it simply, the base class is used to reference the object pointing to the subclass.
Q: Where can polymorphism be used?
A: It can be used in method parameters and return types of methods.
The brother upstairs of the method parameter has provided the code. Here I will show how to use polymorphism in the return type of the method.
In the above example, both JD and Benz are newly developed by ourselves. We can design a factory class to generate a car
/**
* Polymorphism is used in the return type of the method.
* A car can be an abstract class or an interface. JD and Benz inherit this class or implement this excuse respectively.
*/
Public class carfactory {
Public Car Factory (string carname ){
If (carname. Equals ("JD ")){
Return new JD ();
} Else if (carname. Equals ("Benz ")){
Return New Benz ();
} Else {
System. Out. println ("compare, not waiting ");
Return NULL;
}
}
}
This is actually a simple factory model in the design model!
In addition, we can see a lot of applications with polymorphism in JDK. For example, in the equals (Object OBJ) method of the object class, the parameter is an object.
Type parameter. Because the object is the base class of all classes in Java. But when the parameter is passed in, the object of any class can be passed in.
This is a multi-state application!
Using polymorphism can solve the problem of tight coupling in the project and improveProgramClass scalability is a specific implementation of the OCP Principle
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/ssyyll/archive/2008/10/28/3170405.aspx