不知道有没解释对SprinklerSystem类里面定义了soure这一变量
private WaterSource source = new WaterSource();
也就是说source 这个变量属于WaterSource类
然后main程序中System.out.println(sprinklers)会调用
public String toString() {
return
"valve1 = " + valve1 + " " +
"valve2 = " + valve2 + " " +
"valve3 = " + valve3 + " " +
"valve4 = " + valve4 + "\n" +
"i = " + i + " " + "f = " + f + " " +
"source = " + source;
}
这里面"source = " + source;这一句又会调用source这一变量(属于WaterSource类)中的
public String toString() { return s; }
而s的初始值是 Constructed
先送些分给你~~~