多态

1 package duoTai01;
2 
3 public class Animal {
4 
5     public void eat(){
6         System.out.println("动物吃东西");
7     }
8 }
1 package duoTai01;
2 
3 public class Cat extends Animal {
4     @Override
5     public void eat() {
6         System.out.println("猫吃鱼");
7     }
8 }
 1 package duoTai01;
 2 /*
 3     多态:
 4         同一个对象,在不同时刻表现出来的不同形态
 5     举例:猫
 6         可以说猫是猫:猫 cat = new 猫();
 7         也可以说猫是动物:动物 animal = new 猫();
 8         这里猫在不同的时刻表现出来了不同的形态,这就是多态
 9 
10     多态的前提和体现
11         有继承/实现关系
12         有方法重写
13         有父类引用指向子类对象
14  */
15 public class AnimalDemo {
16     public static void main(String[] args) {
17         //有父类引用指向子类对象
18         Animal a = new Cat();
19         a.eat();
20     }
21 }

 

posted @ 2021-01-18 22:15  找不到北的北  阅读(40)  评论(0)    收藏  举报