java: object-oriented-polymorphism

concept

Polymorphism is the ability to have multiple different manifestations or forms of the same behavior.
Embodiment: The same interface uses different instances to perform different operations

achieve

Take the parent class as the formal parameter of the method, and use the object of the subclass as the actual parameter of the method to achieve polymorphism.

Method one: rewrite
Method two: interface
Method three: abstract class and abstract method

Instance

Automatic type conversion (upcast)

Case: Owner and pet play and feed

  • Master human: Master
  • Pet category: Pet --> Dog, Cat
  • Food category: Food --> Bone, Fish

Pets:

public class Pet {
    
    
	String name;
	String sex;
	public Pet {
    
    
		super()
	}

	public Pet (String name, String sex) {
    
    
		this.name = name;
		this.sex = sex;
	}
	
	public void show () {
    
    
		System.out.println("这是一只宠物");
	}
	
	public void play () {
    
    
		System.out.println("这是一只宠物");
	}
}

Dog class:
achieve polymorphism by overriding the method of the parent class

public class Dog extends Pet {
    
    
	String hobby;
	public Dog {
    
    
		super();
	}

	public Dog (String name, String sex, String hobby) {
    
    
		super()
		this.hobby = hobby;
	}
	
	@override
	public void show () {
    
    
		System.out.println("这是一只狗狗,名叫" + name);
	}
	
	@override
	public void play () {
    
    
		System.out.println(name + "正在接飞盘...");
	}
}

public class Cat extends Pet {
    
    
	String breed;
	public Cat {
    
    
		super();
	}

	public Cat (String name, String sex, String breed) {
    
    
		super()
		this.breed = breed;
	}
	
	@override
	public void show () {
    
    
		System.out.println("这是一只猫猫,名叫" + name);
	}
	
	@override
	public void play () {
    
    
		System.out.println(name + "正在走猫步...");
	}
}

Master human:

public Master {
    
    
	String name;
	public Master {
    
    
		super();
	}

	public Master(String name) {
    
    
		super();
		this.name = name;
	}
	
	/*
	public void play (Dog dog) {
		dog.play()
	}
	public void play (Cat cat) {
		cat.play()
	}
	...
	*/
	// 代替为
	public void play (Pet pet) {
    
    
		pet.play()
	}
	// 将父类作为方法的形参(将Pet作为play方法的形参),将子类的对象作为方法的实参(将dog、cat作为play的实参),从而实现多态。
}

test:

public Test {
    
    
	public staic void main(String[] args) {
    
    
		Master master = new Master("唐伯虎");
		Dog dog = new Dog("旺财", "公", "玩游戏");
		master.play(dog);
		
		Dog cat = new Cat("猫儿", "公", "玩游戏");
		master.play(cat);
	}
}
// 旺财正在接飞盘...
// 猫儿正在走猫步...

to sum up

Conditions for polymorphism:

  • Inheritance (inheritance is the basis of polymorphism, there is no polymorphism without inheritance)
  • Override (in polymorphism, the subclass's overridden method is called)
  • The parent class reference variable points to the subclass object: Parent p = new Child();
    take the parent class as the formal parameter of the method (using Pet as the formal parameter of the play method), and use the object of the subclass as the actual parameter of the method (using dog and cat as the actual parameters of play), In order to achieve polymorphism.

Advantages of polymorphism:

  1. Eliminate coupling between types
  2. Replaceability
  3. Scalability
  4. Interface
  5. flexibility
  6. Simplification

Guess you like

Origin blog.csdn.net/weixin_43972437/article/details/113802510