多态
, 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:
- Inheritance (extends or implements)
- Override
- Parent class references subclass objects
The advantages of polymorphism:
- Eliminate coupling between types
- Replaceability
- Scalability
- Interface
- flexibility
- 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();
}
}
You can see the same behavior Call
in different objects Dog
and Cat
have different expressions. The above is my simple understanding of polymorphism.