多态
- 多态对象使用的是父类的成员变量
- 多态对象不能使用子类的特有方法,下转型后才可以使用
- 多态对象下转型后依然可以使用父类中未被子类重写的方法
- 方法的形参是父类,实参可以是该父类的子类
public class Test01 {
public static void main(String[] args) {
Person p = new Chinese();
p.eat();
p.drink();
System.out.println();
Chinese c = (Chinese)p;
c.play();
c.eat();
c.drink();
System.out.println();
test(p);
}
public static void test(Person p){
System.out.println("Test01.test");
}
}
class Person{
public void eat(){
System.out.println("Person.eat");
}
public void drink(){
System.out.println("Person.drink");
}
}
class Chinese extends Person{
@Override
public void eat() {
System.out.println("Chinese.eat");
}
public void play(){
System.out.println("Chinese.play");
}
}
运行结果:
Chinese.eat
Person.drink
Chinese.play
Chinese.eat
Person.drink
Test01.test