abstract class Animal{ //抽象父类 abstract void eat(); //抽象方法 } class Cat extends Animal{ public void eat(){ System.out.println("小猫吃饭"); } public void cathchMouse(){ //子类独有的方法 System.out.println("小猫还会抓老鼠"); } } public class Test01 { public static void main(String[] args) { Cat c1=new Cat(); //子类对象初始化 //实例化子类对象创建一个父类类型对象 此对象拥有多态的特点 Animal a1=new Cat(); // 多态初始化 a1需要去调用父类和子类都有的方法 a1.eat(); //a1.cathMouth(); 不可以调用父类中没有的方法 c1.cathchMouse(); c1.eat(); } }
面向对象多态
最新推荐文章于 2024-07-29 18:00:00 发布