1.多态
1.1多态的概述
什么是多态
同一个对象,在不同时刻表现出来的不同形态
多态的前提
要有继承或实现关系
要有方法的重写
要有父类引用指向子类对象
1.2多态中的成员访问特点
成员访问特点
成员变量
编译看父类,运行看父类
成员方法
编译看父类,运行看子类
动物类:
public class Animal {
public int age = 40;
public void eat() {
System.out.println("动物吃东西");
}
}
猫类:
public class Cat extends Animal {
public int age = 20;
public int weight = 10;
@Override
public void eat() {
System.out.println("猫吃鱼");
}
public void playGame() {
System.out.println("猫捉迷藏");
}
}
测试类:
public class Anim