class C extends B
{
int i = 1;
C(int i){
this.i = i;
System.out.println("C() 's i is "+i);
}
void run(){
System.out.println("C.run() i is "+i);
}
public static void main(String[] args)
{
C c = new C(10);
}
}abstract class B
{
B(){
System.out.println("B() before run()");
run();
System.out.println("B() after run()");
}
abstract void run();
}大家事先预测一下打印内容,再自己编译运行一下,看看结果是不是一样,测试基本功!
{
int i = 1;
C(int i){
this.i = i;
System.out.println("C() 's i is "+i);
}
void run(){
System.out.println("C.run() i is "+i);
}
public static void main(String[] args)
{
C c = new C(10);
}
}abstract class B
{
B(){
System.out.println("B() before run()");
run();
System.out.println("B() after run()");
}
abstract void run();
}大家事先预测一下打印内容,再自己编译运行一下,看看结果是不是一样,测试基本功!
解决方案 »
- 编译spark报错
- JComboBox怎么用啊,怎么选择了一个Item后没有反应啊。
- 一个特别奇怪的问题,大侠答疑
- 卡在BufferederInputStream.read()和HttpURLConnection.getInputStream()运行不下去是为什么,附上代码和DEBUG的资料
- Set<Integer> setA=new HashSet<Integer>(); 通不过!
- 超级菜鸟求助
- 两个byte类型数相加,溢出问题。。。为什么b3是-120.想知道内部转换方式。。。
- 一个奇怪的GC
- jdk1.4安装时都向注册表里写哪些项啊?(希望对你也有启示)
- 有哪位网友知道JpadPro 4.2 Build 611的注册码。拜托给一个,感谢不尽!
- 求打印图形算法
- 哪位高手能给我解释一下这个Java程序的运行结果?
C.run() i is 1
B() after run()
C() 's i is 10
C.run() i is 0
B() after run()
C() 's i is 10
B() before run()
C.run() i is 1
B() after run()
C() 's i is 10
定义i时用static修饰符
C.run() i is 0
B() after run()
C() 's i is 10
加固一下
c()'s i is 10
B() before run()
此时子类C的对象在内存中分配了地址但没有进赋值,还都是默认的初始值。而且在子类C的构造方法C(int i)中参数i覆盖了之前定义的int i;所以此时输出的i是0.即第二行:
C.run() i is 0
顺序执行第三行
B() after run()
此时隐式调用完父类的构造函数,继续执行子类自身的构造函数,C(10)给对象的i赋值为10.
C() 's i is 10
理解的不好的地方请高手指正!谢谢!