向下转型
class Animal{
public void shout(){
System.out.println("喵喵......");
}
}
class Dog extends Animal{ //定义 狗类 , 继承父类 Animal 属性及 方法
@Override
public void shout() {
System.out.println("汪汪......");
}
public void eat(){
System.out.println("吃骨头......");
}
}
class Example1{
public static void main(String[] args) {
Animal an = new Dog(); //向下转型:父类对象一子类对象。
Dog dog = (Dog) an; //对象向下转型: 父类类型 父类对象 = 子类实例;
dog.shout(); // 子类类型 子类对象 =(子类)父类对象:
dog.eat();
}
}