package case8;
public class TestPolymorphism {
public static void main(String[] args) {
// 以多态方式分别实例化子类对象并调用eat()方法
new Dog().eat();
new Cat().eat();
new Lion().eat();
}
}
// Animal类中定义eat()方法
class Animal {
public void eat(){}
}
// Dog类继承Animal类 复写eat()方法
class Dog extends Animal {
public void eat(){
System.out.println("eating bread...");
}
}
// Cat类继承Animal类 复写eat()方法
class Cat extends Animal {
public void eat(){
System.out.println("eating rat...");
}
}
// Lion类继承Animal类 复写eat()方法
class Lion extends Animal {
public void eat(){
System.out.println("eating meat...");
}
}
继承、封装和多态 8使用多态
最新推荐文章于 2023-03-24 07:43:38 发布