上转型对象操作子类继承或重写的方法时,就是通知对应的子类对象去调用这些方法.因此,如果子类重写了父类的某个方法后,对象的上转型对象调用这个方法时,一定是调用这个重写的方法.
可以将对象的上转型对象再强制转换到一个子类对象,这是,该子类对象又具备了子类的所有功能和属性
class ape
{
private int n=100;
void crySpeak(String s)
{
System.out.println(s);
}
}
class People extends ape
{
void computer(int a,int b)
{
int c = a*b;
System.out.println(c);
}
void crySpeak(String s)
{
System.out.println("*** "+s+" ***");
}
}
class Example4_19
{
public static void main(String args[])
{
ape monkey = new People(); //momkey是People对象的上转型对象
monkey.crySpeak("I love this game");
People people = (People)monkey; //把上转型对象强制转化为子类对象
people.computer(10,10);
}
}