日期:2014-05-20  浏览次数:20642 次

“父类名 变量=new 子类名对象”的问题,求指教!
代码如下:
class child{
int i=20 ;
void print(){
System.out.println("child") ;
}

}
class boy extends child{
int i=10 ;
void print(){
System.out.println("boy") ;
}
}
public class test {
public static void main(String[] args){
child num=new boy() ;
System.out.println(num.i) ;
num.print() ;
}
}

输出结果为:
20
boy
不知道为何分别输出父类的成员变量和子类的成员函数,哪位大哥知道的话给小弟讲解一下,谢谢

------解决方案--------------------
楼主弄混淆了吧。。。

函数有多态概念,变量可木有多态的概念。。。
------解决方案--------------------
探讨

引用:
楼主弄混淆了吧。。。

函数有多态概念,变量可木有多态的概念。。。
可是上述程序运行结果就是这样,可以帮我解释一下吗?