//:c04:SimpleConstructor.java
// Demonstration of a simple constructor.
import com.bruceeckel.simpletest.*;class Rock{
Rock(){//This is a constructor
System.out.println("Creating Rock");
}
}public class SimpleConstructor{
static Test monitor = new Test();
public static void main(String[] args){
for(int i = 0;i < 10;i++)
new Rock();
monitor.expect(new String[] {
"Creating Rock",
"Creating Rock",
"Creating Rock",
"Creating Rock",
"Creating Rock",
"Creating Rock",
"Creating Rock",
"Creating Rock",
"Creating Rock",
"Creating Rock"
});
}
} ///:~直接照书上抄的!编译时出错啦!就是那个包的问题:import com.bruceeckel.simpletest.*还有就是static Test monitor = new Test();这句话是干什么用的呀!?貌似没有Test类呀!高人指教!
这句话就是创建一个静态的实例。这个Test可能是项目中存在的,也可能是包中导入进来的。 怎么报错的话,你得仔细检查代码。你贴的代码不全。
这个可以删掉,没用
static Test monitor = new Test();
自己去手动创建个Test类
重写个Test类的方法
monitor.expect(new String[]);
我找到的第4版代码是这样的
//: initialization/SimpleConstructor.java
// Demonstration of a simple constructor.class Rock {
Rock() { // This is the constructor
System.out.print("Rock ");
}
}public class SimpleConstructor {
public static void main(String[] args) {
for(int i = 0; i < 10; i++)
new Rock();
}
} /* Output:
Rock Rock Rock Rock Rock Rock Rock Rock Rock Rock
*///:~
多简单,楼主是从哪儿来的代码啊,弄的这么复杂
明白啦!我说类!上java课老师也没有这么弄过!