当前位置: 技术问答>java相关
紧急: think in java里6.1章里的第二程序(原码见内)执行后,最后一行为什么会显示castille = Constructed
来源: 互联网 发布时间:2015-03-03
本文导语: //: Bath.java // Constructor initialization with composition class Soap { private String s; Soap() { System.out.println("Soap()"); s = new String("Constructed"); } public String toString() { return s; } } public class Bath { ...
//: Bath.java
// Constructor initialization with composition
class Soap {
private String s;
Soap() {
System.out.println("Soap()");
s = new String("Constructed");
}
public String toString() { return s; }
}
public class Bath {
private String
// Initializing at point of definition:
s1 = new String("Happy"),
s2 = "Happy",
s3, s4;
Soap castille;
int i;
float toy;
Bath() {
System.out.println("Inside Bath()");
s3 = new String("Joy");
i = 47;
toy = 3.14f;
castille = new Soap();
}
void print() {
// Delayed initialization:
if(s4 == null)
s4 = new String("Joy");
System.out.println("s1 = " + s1);
System.out.println("s2 = " + s2);
System.out.println("s3 = " + s3);
System.out.println("s4 = " + s4);
System.out.println("i = " + i);
System.out.println("toy = " + toy);
System.out.println("castille = " + castille);
}
public static void main(String[] args) {
Bath b = new Bath();
b.print();
}
} ///:~
下面是该程序的输出:
Inside Bath()
Soap()
s1 = Happy
s2 = Happy
s3 = Joy
s4 = Joy
i = 47
toy = 3.14
castille = Constructed
请问最后一行为什么会显示castille = Constructed
Soap类的构建器只是把私有的字符串变量s赋值Constructed,而并没有打印s的语句,
System.out.println("castille = " + castille);怎么会输出castille = Constructed呢?
而castille = new Soap();最后使castille得到一个什么值呢?Constructed 吗????????????
// Constructor initialization with composition
class Soap {
private String s;
Soap() {
System.out.println("Soap()");
s = new String("Constructed");
}
public String toString() { return s; }
}
public class Bath {
private String
// Initializing at point of definition:
s1 = new String("Happy"),
s2 = "Happy",
s3, s4;
Soap castille;
int i;
float toy;
Bath() {
System.out.println("Inside Bath()");
s3 = new String("Joy");
i = 47;
toy = 3.14f;
castille = new Soap();
}
void print() {
// Delayed initialization:
if(s4 == null)
s4 = new String("Joy");
System.out.println("s1 = " + s1);
System.out.println("s2 = " + s2);
System.out.println("s3 = " + s3);
System.out.println("s4 = " + s4);
System.out.println("i = " + i);
System.out.println("toy = " + toy);
System.out.println("castille = " + castille);
}
public static void main(String[] args) {
Bath b = new Bath();
b.print();
}
} ///:~
下面是该程序的输出:
Inside Bath()
Soap()
s1 = Happy
s2 = Happy
s3 = Joy
s4 = Joy
i = 47
toy = 3.14
castille = Constructed
请问最后一行为什么会显示castille = Constructed
Soap类的构建器只是把私有的字符串变量s赋值Constructed,而并没有打印s的语句,
System.out.println("castille = " + castille);怎么会输出castille = Constructed呢?
而castille = new Soap();最后使castille得到一个什么值呢?Constructed 吗????????????
|
System.out.println("castille = " + castille);
在执行这句话的时候。。。
因为castille不是一个String.所以
castille会自动调用soap类里面的toString()方法。。。
把这个castille转变为一个String...
toString好像是Object里面的一个基本方法。。。。
在执行这句话的时候。。。
因为castille不是一个String.所以
castille会自动调用soap类里面的toString()方法。。。
把这个castille转变为一个String...
toString好像是Object里面的一个基本方法。。。。
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。