- Java code
//: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类呀!
高人指教!
------解决方案--------------------
莫非Thinking in java出第五版了?
我找到的第4版代码是这样的
- Java code
//: 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*///:~