Objective
What does polymorphism mean? Polymorphism is the kind of thing that can have many forms of expression.
Polymorphic three-factor
1. The passive party must have an inheritance relationship
2. Subclasses are generally overriding the parent class method
3. The parameters of the active party's function function must be set to the type of the passive party parent class
For example, a driver drives a car
If the driver can drive a car we need to define a car class and then add a car-driving method to the driver class.
If the driver can drive a truck, we need to define a truck class and then add a truck-driving method to the driver class. Below is a look at the traditional calling method code as follows:
Class Car{private string name;//non-parametric construction method public car () {}//has a parameter construction method public car (String name) {this.name = name;} public void SetName (String name) {this.name = name;} Public String GetName () {return this.name;} public void Run () {}}class bus extends Car{public bus () {}public bus (String name) {super (name);} public void Run () {System.out.println ("bus Running");}} Class Smallcar extends Car{public Smallcar () {}public smallcar (String name) {super (name);} public void Run () {System.out.println ("Coupe Running");}} Class drivers{//If the driver will open bus public void Driverbus (bus b) {b.run ();} If the driver will drive a sedan public void Driversmallcar (Smallcar smcar) {Smcar.run ();}} Class testdt{public static void Main (string[] args) {Drivers d = new Drivers ();//driver to drive bus D.driverbus (new bus ("Bus"));d . Driversmallcar (New Smallcar ("Coupe"));}
As the above-mentioned in the addition of drivers to drive other cars will repeat the above behavior disadvantage is poor scalability, late code maintenance difficult.
Then we use polymorphic notation to optimize the above example code as follows:
Class Car{private string name;//non-parametric construction method public car () {}//has a parameter construction method public car (String name) {this.name = name;} public void SetName (String name) {this.name = name;} Public String GetName () {return this.name;} public void Run () {}}class bus extends Car{public bus () {}public bus (String name) {super (name);} public void Run () {System.out.println ("bus Running");}} Class Smallcar extends Car{public Smallcar () {}public smallcar (String name) {super (name);} public void Run () {System.out.println ("Coupe Running");}} Class Drivers{public void Drivercar (car car) {Car.run ();}} Class testdt{public static void Main (string[] args) {Drivers d = new Drivers ();d. Drivercar (New bus ("Bus")); D.drivercar (New Smallcar ("Coupe"));}}
Compare the first one if the driver will drive another car, just add the class that will drive the car. The driver class does not need to add any additional function methods. Code introduction later maintenance is easier this form of writing polymorphism is also called upward transformation.
Java's multi-state explanation