读书人

类的初始化有关问题

发布时间: 2012-03-01 10:25:46 作者: rapoo

类的初始化问题
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();
}

大家事先预测一下打印内容,再自己编译运行一下,看看结果是不是一样,测试基本功!

[解决办法]
B() before run()
C.run() i is 1
B() after run()
C() 's i is 10

读书人网 >J2SE开发

热点推荐