“父类名 变量=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
不知道为何分别输出父类的成员变量和子类的成员函数,哪位大哥知道的话给小弟讲解一下,谢谢
[解决办法]
楼主弄混淆了吧。。。
函数有多态概念,变量可木有多态的概念。。。
[解决办法]