0 0

看Thinking in java 遇到的数据静态初始化问题15

直接上代码(《thinking in java》 197页的例题):package jxau.edu.cn;
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 StaticTest {
public static void main(String args[]){
System.out.println("create a  new Cupboard in main");
new Cupboard();
System.out.println("create a  new Cupboard in main");
new Cupboard();
    // Table t2=new Table();
// t2.f2(1);
//t3.f3(1);
}
}
我运行的结果是这样的:create a  new Cupboard in main
Bowl(4)
Bowl(5)
Bowl(3)
Cupboard()
f(2)
create a  new Cupboard in main
Bowl(3)
与书上的结果很不一样,求大神给看看! [color=red][/color]

问题补充:
Rainbow702 写道
少了这个吧
引用
Cupboard()
f(2)

看看是不是你的 Eclipse 的 Console 窗口太小没有显示完全

不是,书上运行的结果有对静态数据Bowl(1)Bow(2)的初始化,而我运行的结果是没有的!

问题补充:
Rainbow702 写道
我刚去看了一下你说的章节,里面是有下面这两句的啊
引用

static Table t2 = new Table();
static Cupboard t3 = new Cupboard();

这样的话 问题就明朗了 ! 我的书是盗版的 上面真心没有上两句!
OO 
2011年10月05日 19:43

3个答案 按时间排序 按投票排序

0 0

采纳的答案

我刚去看了一下你说的章节,里面是有下面这两句的啊

引用

static Table t2 = new Table();
static Cupboard t3 = new Cupboard();

2011年10月06日 15:29
0 0

引用
有对静态数据Bowl(1)Bow(2)的初始化

不可能吧?你的Table都没有被使用(或者说,没有被加载),它的这两个变量怎么会被初始化呢?

2011年10月06日 15:19
0 0

少了这个吧

引用
Cupboard()
f(2)

看看是不是你的 Eclipse 的 Console 窗口太小没有显示完全

2011年10月06日 00:05

相关推荐

Global site tag (gtag.js) - Google Analytics