class Father
{
int m=5;
public Father(){this.show();}
void show(){System.out.println("父");}
}
class Child1 extends Father
{
int m=10;void show(){System.out.println(m);}
}
class Child2 extends Father
{void show(){System.out.println(m);}
}
class Test
{
public static void main(String args[])
{
Father n=new Child1();Father n1=new Child2();}
}
{
int m=5;
public Father(){this.show();}
void show(){System.out.println("父");}
}
class Child1 extends Father
{
int m=10;void show(){System.out.println(m);}
}
class Child2 extends Father
{void show(){System.out.println(m);}
}
class Test
{
public static void main(String args[])
{
Father n=new Child1();Father n1=new Child2();}
}
解决方案 »
- 救命啊,各位老大,关于一个java的GUI的一个问题。。。。。。。。。。。。。。。。
- 关于事件监听
- 如何在单击b类中的按钮执行代码的过程中,同时让a类中的一个按钮下的代码也执行。
- 只让一个java程序实例运行
- 救救我!****急切想获取一些SNMP的资源*******
- 关于扩充Applet的权限的问题,这个问题有人研究过么?
- 数据源有时正常,有时抛出异常!!!!!!!!!!!
- 这里有搞Grid的朋友吗或Globus
- 为什么说URLConnection类不能存在呢?
- 紧急求助:没有网卡的pc,如何安装网络协议
- 现在有多少人在 NetBeans IDE 中设计 Swing GUI?
- 请教JAVA如何打印EXCEL文档
比如:Child1 c1=new Child1();
c1会有两个int型的域:super.m和m(也就是this.m)。
2.构造子类对象的时候,会先调用父类的构造方法,对继承自父类的域进行初始化,然后再对子类定义的域
进行初始化。
可以用下面的代码验证这个过程:
public class Test{
public static void main(String args[])
{
//执行Father()==>Child1.show():此时父类的构造方法还未结束,子类的域m还没有初始化
Father n1=new Child1();
Father n2=new Child2();
}
}class Father{
int m=5;
public Father(){
System.out.println("father constructor start");
this.show();
System.out.println("father constructor end");
}
void show(){
System.out.println("father show");
System.out.println("父");
}
}class Child1 extends Father{
int m = 6;
public Child1(){
System.out.println("child1 constructor start");
this.show();
System.out.println("child1 constructor end");
}
void show(){
System.out.println("child1 show start");
System.out.println(m);
System.out.println("child1 show end");
}
}class Child2 extends Father{
void show(){System.out.println(m);}
}
看看这顺序,在父类构造方法没完成时 子类m=10其实还同有执行到,也就是给值也没有用,打印出来的应该是int的默认值0