2018-03-19
Subclasses can inherit the object methods of the parent class. After inheritance, providing this method repeatedly is called method rewriting; it is also called override.
1. Parent class Item
The parent class Item has a method called effect
package property; public class Item { String name; int price; public void buy(){ System.out.println("purchase"); } public void effect() { System.out.println("After use, it can be effective."); } }
2. Subclass LifePotion
Subclass LifePotion inherits Item and also provides method effect
package property; public class LifePotion extends Item{ public void effect(){ System.out.println("After using the blood bottle, the blood can be returned."); } }
3. Call the method of rewriting
Call the overridden method
Calls will execute overridden methods, not methods from the parent class
So the effect of LifePotion prints:
"After using the blood bottle, the blood can be returned."
package property; public class Item { String name; int price; public void effect(){ System.out.println("After use, it can be effective."); } public static void main(String[] args) { Item i = new Item(); i.effect(); LifePotion lp =new LifePotion(); lp.effect(); } }
4. What if there is no such mechanism to rewrite?
Without such a mechanism, that is, LifePotion class, once Item is inherited, all methods cannot be modified.
But LifePotion also wants to provide a little different functionality. To achieve this goal, it can only abandon inheriting Item, rewrite all attributes and methods, and then make minor changes when writing effect s.
This increases development time and maintenance costs.
package property; public class Item { String name; int price; public void buy(){ System.out.println("purchase"); } public void effect() { System.out.println("After use, it can be effective."); } }
package property; public class LifePotion { String name; int price; public void buy(){ System.out.println("purchase"); } public void effect(){ System.out.println("After using the blood bottle, the blood can be returned."); } }
5. Polymorphism
Operator polymorphism: +can be used as an arithmetic operation or as a string connection.
Class polymorphism: parent class references point to child class objects
If both sides of + are integers, then + represents the sum of numbers.
If either side of the + sign is a string, then + represents a string connection
package charactor; public class Hero { public String name; protected float hp; public static void main(String[] args) { int i = 5; int j = 6; int k = i+j; //If+Both sides of the number are integer, so+Representation number addition System.out.println(k); int a = 5;
String b = "5"; String c = a+b; //If+On either side of the number, any one is a string, so+Represents string concatenation System.out.println(c); } }
Observational polymorphism:
1. Both I 1 and i2 are Item types
2. Call the effect method
3. Output of different results
Polymorphism: All of them are of the same type, calling the same method and presenting different states
package property; public class Item { String name; int price; public void buy(){ System.out.println("purchase"); } public void effect() { System.out.println("After use, it can be effective. "); } public static void main(String[] args) { Item i1= new LifePotion(); Item i2 = new MagicPotion(); System.out.print("i1 yes Item Type, execution effect Printing:"); i1.effect(); System.out.print("i2 Also Item Type, execution effect Printing:"); i2.effect(); } }
package property; public class LifePotion extends Item { public void effect(){ System.out.println("After using the blood bottle, the blood can be returned."); } }
package property; public class MagicPotion extends Item{ public void effect(){ System.out.println("When the blue bottle is used, it can return to magic."); } }
Class polymorphism condition:
To achieve class polymorphism, you need the following conditions
1. Parent Class (Interface) References Point to Subclass Objects
2. The methods invoked are Rewrite
So what does polymorphism do? By comparison No use of polymorphism and Use polymorphism To further understand
I. Polymorphism of Classes - Non-use of Polymorphism
If you don't use polymorphism, assuming that heroes want to use blood bottles and magic bottles, you need to design two methods for Hero.
useLifePotion and useMagicPotion
There are many other things besides blood bottles and magic bottles, so there are many ways to design them, such as
Use PurityPotion to Purify Pharmaceutical Water
useGuard Guard
Use Invisible Potion with Invisible Potions
package charactor; import property.LifePotion; import property.MagicPotion; public class Hero { public String name; protected float hp; public void useLifePotion(LifePotion lp){ lp.effect(); } public void useMagicPotion(MagicPotion mp){ mp.effect(); } public static void main(String[] args) { Hero garen = new Hero(); garen.name = "Galen"; LifePotion lp =new LifePotion(); MagicPotion mp =new MagicPotion(); garen.useLifePotion(lp); garen.useMagicPotion(mp); } }
Class Polymorphism-Usage Polymorphism
If there are many kinds of items, there are many ways to design them.
For example, use Armor, use Weapon, etc.
At this point, polymorphism is used to solve this problem; a method called useItem is designed, and its parameter type is Item.
If you use a blood bottle, call this method
If you use the magic bottle, or call the method
No matter what kind of object the hero wants to use, only one method is needed.
package charactor; import property.Item; import property.LifePotion; import property.MagicPotion; public class Hero { public String name; protected float hp; public void useItem(Item i){ i.effect(); } public static void main(String[] args) { Hero garen = new Hero(); garen.name = "Galen"; LifePotion lp =new LifePotion(); MagicPotion mp =new MagicPotion(); garen.useItem(lp); garen.useItem(mp); } }