首先是成员函数在多态中调用时,编译看左边,运行看右边。其结果不是一样的。例如:class Fu
{
static String age()
{
return " I am a old man";
}
String name()
{
return "LaoZi";
}
}
class Zi extends Fu
{
static String age()
{
return "I am a young man";
}
String name()
{
return "ErZi";
}
}
class Test
{
public static void main(String[] args)
{
Zi s = new Zi(); //(1)
Fu s=new Zi(); //(2)
System.out.println(s.age() + ", " + s.name());
}
}
//(1)输出结果为 I am a young man ErZi
//(2) 输出结果为 I am a old man Erzi |