public class ff { public ff() {
// TODO Auto-generated constructor stub
} /**
* @param args
*/
public static void main(String[] args) {
new clss();
new son();
}}
class son extends father{
int x = 100; public son() {
super();
}
public void dothing()
{
System.out.println(x);
}
}
class father
{
public void dothing()
{
}
public father()
{
dothing();
}
}class clss
{
int x = 100;
public clss()
{
System.out.println(x);
}
}
大家觉得打印出来值是多少
100
0为什么第二个把值取不出来啊
// TODO Auto-generated constructor stub
} /**
* @param args
*/
public static void main(String[] args) {
new clss();
new son();
}}
class son extends father{
int x = 100; public son() {
super();
}
public void dothing()
{
System.out.println(x);
}
}
class father
{
public void dothing()
{
}
public father()
{
dothing();
}
}class clss
{
int x = 100;
public clss()
{
System.out.println(x);
}
}
大家觉得打印出来值是多少
100
0为什么第二个把值取不出来啊
public void dothing()
{
System.out.println(x);
} //此时还没走到x的初始化语句因此x是默认值0。如果x是字符类型的话就是null了
其次,先走父类的构造函数,再走子类的构造函数,只有走子类的构造函数之前,才会初始化子类的 int x = 100;语句.
我一会上.net试下,看看也是不是这样的.