Object-oriented multi-state understanding, the role and benefits of multi-state, object-oriented multi-state
What is polymorphism?
In short, it refers to the object pointing to the subclass with the reference of the parent class. Polymorphism can be understood as "one interface and multiple implementations", which means multiple forms of the same thing. polymorphism allows sub-class objects to be used as parent objects, A reference of a parent type points to an object of its child type. The method called is the method of this child type.
What is the role of polymorphism?
Considering different subclass objects as parent classes, we can avoid the differences between different subclass objects, write common code, and make general programming to adapt to the changing needs.
Looking at the example below, programmers prefer to use code to illustrate the problem.
For example, we use our program to implement the fight against Japanese devils by Chinese soldiers:
For example, in 1900, the Chinese military used a big knife to fight the enemy. According to OO's ideas, we would abstract two classes, one representing the big knife and the other representing the military, that is to say, a soldier uses a big knife to kill the enemy. The weapon has a kill () method, which indicates killing Japanese devils. The soldier has a way to fight with a big knife. Our implementation method is as follows:
Public class Sword {public void kill () {System. out. println ("kill the Japanese devil's head! ") ;}} Public class Soldier {public void fight (Sword sword) {sword. kill ();} public static void main (String args []) {Soldier soldier = new Soldier (); Sword sword = new Sword (); soldier. fight (sword );}}View Code
If you write such code, congratulations! 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!
Think about it,
If our demand changes and the Japanese devils invaded China again in 1939, what should we do? We can't fight with a big knife like before, in that case, will the War of Resistance Against Japanese Aggression not be over yet ?!
So what should we do with the previous system in our program? So 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 gun class and a kill () method.
Public class Gun {public void kill () {System. out. println ("shot into the head of a Japanese devil! ");}}View Code
Our core business class Soldier also needs to be upgraded
Public class Soldier {/* public void fight (Sword sword) {sword. kill ();} */public void fight (Gun gun) {gun. kill ();} public static void main (String args []) {Soldier soldier = new Soldier (); // Sword sword = new Sword (); // soldier. fight (sword); Gun gun = new Gun (); soldier. fight (gun );}}View Code
Is the coupling too strong? In the future, we can abstract large knives and guns as weapons:
Public abstract class Weapon {public void kill (); // Let the subclass run} public Sword extends Weapon {@ Override public void kill () {System. out. println! ") ;}} Public Gun extends Weapon {@ Override public void kill () {System. out. println (" shoot a bullet into the head of a Japanese devil !); } Public class Soldier {private Weapon weapon; public Soldier () {} public Soldier (Weapon weapon) {this. weapon = weapon;} public void setWeapon (Weapon weapon) {this. weapon = weapon;} public void fight () {weapon. kill ();} public static void main (String args []) {Soldier soldier = new Soldier (); Weapon sword = new Sword (); soldier. setWeapon (sword); soldier. fight (); // In December 1900, the attacker killed Weapon gun = new Gun (); soldier. setWeapon (gun); soldier. fight (); // use a Xiaomi + rifle to kill an enemy on March 13, 1939 }}View Code
Where can polymorphism be used?
It can be used in the parameters of the method and the return type of the method.
The code above shows how polymorphism is used in the return type of the method.
In the above example, both Sword and Gun are newly created by ourselves. We can design a factory class to generate weapons.
Public class WeaponFactory {public Weapon createWeapon (String year) {if (year. equals ("1900") {return new Sword ();} else if (year. equals ("1939") {return new Gun () ;}else {return null ;}} public class Soldier {private Weapon weapon; public Soldier () {} public Soldier (Weapon weapon) {this. weapon = weapon;} public void setWeapon (Weapon weapon) {this. weapon = weapon;} public void fight () {weapon. kill ();} public static void main (String args []) {Soldier soldier = new Soldier (); WeaponFactory weaponFactory = new WeaponFactory (); Weapon sword = weaponFactory. createWeapon ("1900"); soldier. setWeapon (sword); soldier. fight (); // In December 1900, the attacker killed Weapon gun = weaponFactory. createWeapon ("1939"); soldier. setWeapon (gun); soldier. fight (); // use a Xiaomi + rifle to kill an enemy on March 13, 1939 }}View Code
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!
Polymorphism can solve the problem of tight coupling in the project and improve the scalability of the program class. It is a specific implementation of the OCP principle.