java编程思想程序研究不懂,望解释

zhigangsun 2005-03-02 02:42:46
第四章关于静态变量初始化顺序的程序,代码如下: class Bowl {
Bowl(int marker) {
System.out.println("Bowl(" + marker + ")");
}

void f(int marker) {
System.out.println("f(" + marker + ")");
}
}

class Table {
static Bowl b1 = new Bowl(1);
Table() {
System.out.println("Table()");
b2.f(1);
}

void f2(int marker) {
System.out.println("f2(" + marker + ")");
}

static Bowl b2 = new Bowl(2);
}

class Cupboard {
Bowl b3 = new Bowl(3);
static Bowl b4 = new Bowl(4);
Cupboard() {
System.out.println("Cupboard()");
b4.f(2);
}

void f3(int marker) {
System.out.println("f3(" + marker + ")");
}

static Bowl b5 = new Bowl(5);
}

public class StaticInitialization {
// static Test monitor = new Test();
public static void main(String[] args) {
System.out.println("Creating new Cupboard() in main");
new Cupboard();
System.out.println("Creating new Cupboard() in main");
new Cupboard();
t2.f2(1);
t3.f3(1);
/*monitor.expect(new String[] {
"Bowl(1)",
"Bowl(2)",
"Table()",
"f(1)",
"Bowl(4)",
"Bowl(5)",
"Bowl(3)",
"Cupboard()",
"f(2)",
"Creating new Cupboard() in main",
"Bowl(3)",
"Cupboard()",
"f(2)",
"Creating new Cupboard() in main",
"Bowl(3)",
"Cupboard()",
"f(2)",
"f2(1)",
"f3(1)"
});*/
}

static Table t2 = new Table();
static Cupboard t3 = new Cupboard();
} ///:~
...全文
220 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
angues1980 2005-03-02
  • 打赏
  • 举报
回复
一点技术含量都没有,哼~
zhyjxy 2005-03-02
  • 打赏
  • 举报
回复
...........
angues1980 2005-03-02
  • 打赏
  • 举报
回复
晕,楼上的什么意思啊?盗版这么露骨还要连盗两遍才过瘾啊?
zxmzfbdc 2005-03-02
  • 打赏
  • 举报
回复
1、根据java编程思想“static成员只有在需要的时候进行初始化“,例如:如果没有创建Myclass对象,就永远不可能用到Myclass.b1,因此也不会去创建static的Bowl b1和b2。
所以这里程序首先执行static Table t2 = new Table();显示Bowl(1) Bowl(2) Table() f(1)
然后才执行static Cupboard t3 = new Cupboard();显示Bowl(4) Bowl(5) Bowl(3)
zxmzfbdc 2005-03-02
  • 打赏
  • 举报
回复
1、根据java编程思想“static成员只有在需要的时候进行初始化“,例如:如果没有创建Myclass对象,就永远不可能用到Myclass.b1,因此也不会去创建static的Bowl b1和b2。
所以这里程序首先执行static Table t2 = new Table();显示Bowl(1) Bowl(2) Table() f(1)
然后才执行static Cupboard t3 = new Cupboard();显示Bowl(4) Bowl(5) Bowl(3)
angues1980 2005-03-02
  • 打赏
  • 举报
回复
所以在创建第一个Table t2的时候,并没有创建Cupboard t3,当然Bowl(4) Bowl(5) ……会在f(1)之后了。
angues1980 2005-03-02
  • 打赏
  • 举报
回复
实例化的时候应该按照先初始化静态成员然后初始化非静态成员,然后在调用构造函数的顺序。
编程思想里说的很清楚了“static成员只有在需要的时候进行初始化,如果没有创建Table对象,就永远不可能用到Table.b1或者Table.b2,因此也不会去创建static的Bowl b1和b2。”
所以这里程序首先执行static Table t2 = new Table();显示Bowl(1) Bowl(2) Table() f(1)
然后才执行static Cupboard t3 = new Cupboard();显示Bowl(4) Bowl(5) Bowl(3)

zhigangsun 2005-03-02
  • 打赏
  • 举报
回复
你先贴出你认为的顺序来,我认为应该是Bowl(1);Bowl(2);Bowl(4);Bowl(5);Table();f(1);Bowl(3);Cupboard();f(2);
sammyjeep 2005-03-02
  • 打赏
  • 举报
回复
楼主分析的结果是什么样子的啊??
强长黑粗硬 2005-03-02
  • 打赏
  • 举报
回复
不就是如果有静态变量就先处理静态变量,然后再处理non-static变量吗?楼主想问什么呀?
zhigangsun 2005-03-02
  • 打赏
  • 举报
回复
我分析的输出顺序还运行结果不一样呢怎么样。说说你的结果,分析以下
angues1980 2005-03-02
  • 打赏
  • 举报
回复
你的问题呢?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧