What this article brings to you is what is java polymorphism? The use of java polymorphism, through some functions in the game, will help you master the use of polymorphism. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
In a battle game (any similarities are purely coincidental), there are two different mage heroes: Xiao Qiao and Daji.
Both mage heroes have attack methods. Xiao Qiao's attack damage is 10 and consumes 20 mana. Daji's attack damage is 15 and consumes 30 mana. Players can operate two heroes to attack. Let’s take a look at the implemented code.
Parent class-Hero: whyusepolymorphic.Hero.java
package whyusepolymorphic; public class Hero { private int magicPoint;//魔法值 private int hurt;//伤害 private String name;//姓名 public Hero(int magicPoint, int hurt, String name) { super(); this.magicPoint = magicPoint; this.hurt = hurt; this.name = name; } public int getMagicPoint() { return magicPoint; } public void setMagicPoint(int magicPoint) { this.magicPoint = magicPoint; } //省略属性的 getter 和 setter 方法 }
Subclass-Little Joe: whyusepolymorphic.LittleJoe.java
package whyusepolymorphic; public class LittleJoe extends Hero { public LittleJoe(int magicPoint, int hurt, String name) { super(magicPoint, hurt, name); } //攻击的方法 public void attack() { System.out.println(this.getName()+" 发动攻击,伤害为:"+this.getHurt() +"。消耗 20的魔法值"); this.setMagicPoint(getMagicPoint()-20);//魔法值-20 } }
Subclass-Daji:whyusepolymorphic.Daji.java
package whyusepolymorphic; public class Daji extends Hero{ public Daji(int magicPoint, int hurt, String name) { super(magicPoint, hurt, name); } public void attack() { System.out.println(this.getName()+" 发动攻击,伤害为:"+this.getHurt() +"。消耗 30的魔法值"); this.setMagicPoint(getMagicPoint()-30);//魔法值-30 } }
Player:whyusepolymorphic.Player.java
package whyusepolymorphic; public class Player { public void play(LittleJoe littleJoe) { littleJoe.attack(); } public void play(Daji daji) { daji.attack(); } }
The above code is complete After realizing the requested function, we know that there cannot be only these few heroes. What if new magic heroes are added later and the damage is different?
We can add new classes to implement attack methods, and modify the player class to add methods for operating heroes. This method can meet the needs of Hero expansion, but if more Heroes are added later, it will not be so convenient for us to maintain.
Studying the above code, we found that the parameters of the play method in the Player class are all subclasses of the Hero class. Can one play(Hero hero) method be used to operate all heroes? This optimized design can be achieved using polymorphism.
Concisely, polymorphism means multiple forms. Polymorphic forms of carbon in nature include graphite, diamond, etc. The action of cutting includes paper cutting, hair cutting, etc. The same operation produces different results due to different conditions.
Then polymorphism in the program refers to the same reference type, using different instances to perform different operations (the parent class reference specifies the subclass objectHero h=new Daji();
).
Steps to implement polymorphism:
1. Write parent classes and subclasses# with inheritance relationships
##2.The subclass overrides the parent class method
3. Use thereference of the parent class to point to the object of the subclass
The parent class serves as Method parameters implement polymorphismUse polymorphism to optimize the above codeModify Hero.java to add attack methodspackage whyusepolymorphic; public class Hero { //省略属性和构造方法 //攻击的方法 public void attack() { System.out.println(this.getName()+" 发动攻击,伤害为:"+this.getHurt() +"。消耗 20的魔法值"); this.setMagicPoint(getMagicPoint()-20);//魔法值-20 } //省略 getter 和 setter 方法 }
package whyusepolymorphic; public class Player { public void play(Hero hero) { hero.attack(); } }
package whyusepolymorphic; public class TestPlay { public static void main(String[] args) { Player p=new Player(); Hero daji=new Daji(100,15,"妲己"); p.play(daji); System.out.println(daji.getName()+" 剩余魔法:"+daji.getMagicPoint()); Hero littleJoe=new LittleJoe(100,10,"小乔"); p.play(littleJoe); System.out.println(littleJoe.getName()+" 剩余魔法:"+littleJoe.getMagicPoint()); } }
package whyusepolymorphic; public class Player { public void play(Hero hero) { hero.attack(); } public Hero getHero(int id) { if(1==id) { return new Daji(100,15,"妲己"); }else if(2==id){ return new LittleJoe(100,10,"小乔"); }else { System.out.println("没有这个英雄"); return null; } } }
package whyusepolymorphic; import java.util.Scanner; public class TestPlay { public static void main(String[] args) { Player p=new Player(); System.out.println("欢迎来到英雄商店,请选择要购买的英雄:1.妲己2.小乔"); Scanner input=new Scanner(System.in); int id=input.nextInt(); Hero h=p.getHero(id); if(null!=h) { h.attack(); } } }
package whyusepolymorphic; public class Daji extends Hero{ //省略构造方法及之前其他方法 public void queenWorship() { System.out.println("释放大招:女王崇拜"); } }
package whyusepolymorphic; public class LittleJoe extends Hero { //省略构造方法及之前其他方法 public void dazzlingStar() { System.out.println("释放大招:星华缭乱"); } }
package whyusepolymorphic; public class Player { //省略构造方法及之前其他方法 public void bigMove(Hero hero) { hero.dazzlingStar(); } }
hero.dazzlingStar(); Error reporting
Hero joe=new LittleJoe(100,10,"小乔"); Daji daji=(Daji) joe;
对象 instanceof 类或接口
public void bigMove(Hero hero) { if (hero instanceof Daji) { ((Daji)hero).queenWorship(); }else if(hero instanceof LittleJoe) { ((LittleJoe)hero).dazzlingStar(); } }
Player p=new Player(); p.bigMove(new LittleJoe(100,10,"小乔")); p.bigMove(new Daji(100,15,"妲己"));
Java video tutorial, java development graphic tutorial, bootstrap video tutorial!
The above is the detailed content of What is java polymorphism? The use of java polymorphism. For more information, please follow other related articles on the PHP Chinese website!