To learn Java, you need to know these-polymorphism of the three major characteristics

多态, As the name implies 多种状态. To put it simply, it's an 同一个接口使用不同的实例来执行不同操作abstraction: both cats and dogs bark, but they sound differently.
多态The existence of must meet the following conditions:

  1. Inheritance (extends or implements)
  2. Override
  3. Parent class references subclass objects

The advantages of polymorphism:

  1. Eliminate coupling between types
  2. Replaceability
  3. Scalability
  4. Interface
  5. flexibility
  6. Simplification
    The following code is used as an example:
public class Animal {

	String color;
	String name;
	public void Call() {
		System.out.println("动物叫");
	}
	public void getName() {
		System.out.println("动物名");
	}
}
class Cat extends Animal{
	public void Call() {
		System.out.println("喵喵喵");
	}
}
class Dog extends Animal{
	public void Call() {
		System.out.println("汪汪汪");
	}
	public static void main(String []args) {
		Animal dog = new Dog();
		Animal cat = new Cat();
		dog.getName();
		dog.Call();
		cat.Call();
	}
}

Insert picture description here
You can see the same behavior Callin different objects Dogand Cathave different expressions. The above is my simple understanding of polymorphism.

Published 470 original articles · Like 437 · Visit 230,000+

Guess you like

Origin blog.csdn.net/qq_41505957/article/details/105652317