比如我这个程序
public class Testjava{
public static void main(String args[])
{
fun(new Student());
fun(new Allen());
}
public static void fun(Person p)
{
p.fun2();
if (p instanceof Student)
{
Student s=(Student)p;
s.fun3();
}
if (p instanceof Allen)
{
Allen a=(Allen)p;
a.fungod();
}
}
}
fun3()和fungod()分别是Student类和Allen类里面的方法,我想调用他们的时候为什么要向上转型呢,感觉没什么意义,直接写Student s=new Student()和Allen a=new Allen()各建立一个新对象再调用他们各自特有的方法不就可以了吗?可否详细解释向上转型在JAVA中的意义。我是自学的,麻烦解释详细一点,拜托大家了