JAVA-Interface and Inheritance (4) Polymorphism

Operator polymorphism

The same operator has different functions in different situations.
If both sides of the + sign are integers, then + represents the addition of numbers.
If any one on both sides of the + sign is a string, then + represents string concatenation

public class test {
    
    

	public static void main(String[] args) {
    
    
		int a  = 5;
		String b = "5";
		String c = a+b;
		System.out.println(c);
	}
}

Observational polymorphism

package property;

public class item {
    
    
	String name;
	int price;
	public void buy() {
    
    
		System.out.println("购买了" + name);
	}
	public void effect() {
    
    
		System.out.println("购买后的效果");
	}
	public static void main(String args[]) {
    
    
		item a  = new lifepotion();
		item b = new magicpotion();
		a.effect();
		b.effect();
	}
}

Class polymorphism

To achieve class polymorphism, the following conditions are required

  1. The parent class (interface) reference points to the child class object
  2. The method called is overridden
    So what is the role of polymorphism? Learn more by comparing not using polymorphism and using polymorphism

Class polymorphism-do not use polymorphism

Suppose the hero wants to use the blood bottle and the magic bottle, he needs to design two methods for Hero
useLifePotion
useMagicPotion

In addition to blood bottles and magic bottles, there are many other items, so you need to design many, many methods, such as
usePurityPotion,
useGuard,
useInvisiblePotion, use invisible potion
, etc.

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 = "盖伦";
     
        LifePotion lp =new LifePotion();
        MagicPotion mp =new MagicPotion();
         
        garen.useLifePotion(lp);
        garen.useMagicPotion(mp);
         
    }
       
}

Class polymorphism-use polymorphism

package property;
import property.item;
import property.lifepotion;
import property.magicpotion;

public class hero {
    
    
	String name;
	int hp;
	public hero(String name,int hp) {
    
    
		this.name = name;
		this.hp = hp;
	}
	public void useitem(item i) {
    
    
		i.effect();
	}
	public static void main(String args[]) {
    
    
		hero a = new hero("德玛西亚",100);
		
		lifepotion i = new lifepotion();
		magicpotion b = new magicpotion();
		a.useitem(i);
		a.useitem(b);
	}
}

Exercise-Polymorphism ⭐⭐⭐⭐If
there are many types of items, then you need to design a lot of methods,
such as useArmor, useWeapon, etc.

At this time, polymorphism is used to solve this problem.
Design a method called useItem whose parameter type is Item.
If you use a blood
bottle, call this method. If you use a magic bottle, or call this method,
no matter what kind of item the hero wants to use, you only need One way

package charactor;
public class hero implements AD{
    
    
	String name;
	int hp;
	float movespeed;
	int armor;
	public void physicattack() {
    
    
		System.out.println("物理攻击");
	}
	public void die() {
    
    
		
	}
	public void kill(hero i) {
    
    
		i.die();
	}
	public static void main(String args[]) {
    
    
		hero a = new adhero();
		a.die();
		hero b = new aphero();
		b.die();
	}
}

1. Implement the interface in the adhere, aphero, and adaphero classes, implements mortal
(the method must be called to implement the interface)

2. Override the die method in the adhereo, aphero, and adaphero classes. Of course, they must have this method die() in their parent class.

3. Create a new method in the parent class, I use kill() here, and the parameter is the hero type,
which is the parent class of adhere, aphero, and adaphero.

public void kill(hero i){
    
    
	i.die()
}

To achieve class polymorphism, the following conditions are required :

  1. The parent class (interface) reference points to the child class object
  2. The method called is overridden **

Guess you like

Origin blog.csdn.net/qq_17802895/article/details/108533996