以下均为菜鸟俺所言:
Think In Java书中,作者编写的一些示例可以帮助我们很好的去理解文中大意,调试好程序后再Debug运行看程序的运行流程会让我思路清晰,书中所言也明朗了许多。
作者在书中的示例都会引用他自己编写的应用类库com.bruceeckel.simpletest.*,我是JDK 6环境,貌似此类库是在JDK1.4环境下编写的,所以老是编译报错,我也没能耐去更改。就采取折中的方法,不引用作者自定义的类库,去除导入类库的声明和几句相关语句,举例第四章This关键字的源代码 如下:
//这是原源代码
import com.bruceeckel.simpletest.*;
public class Flower {
static Test monitor = new Test();
int petalCount = 0;
String s = new String("null");
Flower(int petals) {
petalCount = petals;
System.out.println("Constructor w/ int arg only, petalCount= "
+ petalCount);
}
Flower(String ss) {
System.out.println("Constructor w/ String arg only, s=" + ss);
s = ss;
}
Flower(String s, int petals) {
this(petals);
// ! this(s); // Can't call two!
this.s = s; // Another use of "this"
System.out.println("String & int args");
}
Flower() {
this("hi", 47);
System.out.println("default constructor (no args)");
}
void print() {
// ! this(11); // Not inside non-constructor!
System.out.println("petalCount = " + petalCount + " s = " + s);
}
public static void main(String[] args) {
Flower x = new Flower();
x.print();
monitor.expect(new String[] {
"Constructor w/ int arg only, petalCount= 47",
"String & int args", "default constructor (no args)",
"petalCount = 47 s = hi" });
}
}
//这是修改后的代码
public class Flower {
int petalCount = 0;
String s = new String("null");
Flower(int petals) {
petalCount = petals;
System.out.println("Constructor w/ int arg only, petalCount= "
+ petalCount);
}
Flower(String ss) {
System.out.println("Constructor w/ String arg only, s=" + ss);
s = ss;
}
Flower(String s, int petals) {
this(petals);
// ! this(s); // Can't call two!
this.s = s; // Another use of "this"
System.out.println("String & int args");
}
Flower() {
this("hi", 47);
System.out.println("default constructor (no args)");
}
void print() {
// ! this(11); // Not inside non-constructor!
System.out.println("petalCount = " + petalCount + " s = " + s);
}
public static void main(String[] args) {
Flower x = new Flower();
x.print();
}
}
在此也希望能有前辈指点我如何能顺利调试作者编写的源代码!谢谢。