解决方案 »
- HTMLParser+正则表达式解析本地网页?????(求助)
- 急急急!求给位高手帮我完善一下 散分!!!!!!
- jTable的显示问题
- 一个笔试常考题不懂,望高手解答,谢谢!
- 求助,Swing中窗口JFrame如何输入点和线段
- 如何跟踪程序执行的详细信息
- 我试图想用actionPerformed(ActionEvent e) ,但是很不幸………… 高手请指点迷津
- 【各位大哥帮帮忙,关于在EditPlus下编译和执行java的问题】
- 哪位知道和ERP相关的SAT和ABAT是什么意思?现场给分
- socket传输二进制文件输出是不是要用DataOutputStream?
- 只指定数据包要发送的端口,怎么接收不了?
- JComboBox展开的情况下其它组件无法获得鼠标点击事件,触发的鼠标的事件会被用来处理JComboBox弹出部分的隐藏
Flower() {
this("hi", 47);
print("default constructor (no args)");
}
在含有两个参数的构造函数中只调用了参数为int型的构造函数,你的String ss没有调用到,所有不会有输出。
Flower(String s, int petals) {
this(petals);
//! this(s); // Can’t call two!
this.s = s; // Another use of "this"
print("String & int args");
}
Flower(String ss) {
print("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"
print("String & int args");
}
这一句不是调用String s么?
那么为什么第一个含int参数的构造函数能被调用?
第一个含int的构造函数能被调用是不是因为第一句就赋值了,所以后面能被调用.
第二个含String的构造函数不被调用是不是因为第一句没有赋值,而是在第二句才赋值,所以不行?
{
int petalCount = 0;
String s = "initial value"; 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); //第三步:由于petals变量是int类型的,所以调用构造方法Flower(int petals);
this.s = s;
System.out.println("String & int args");
} Flower()
{
this("hi", 47); //第二步:由于是两个参数,调用Flower(String s, int petals);
System.out.println("default constructor (no args)");
} void printPetalCount()
{
System.out.println("petalCount = " + petalCount + " s = " + s);
} public static void main(String[] args)
{
Flower x = new Flower(); //第一步:调用无参数构造方法Flower();
x.printPetalCount(); //第四步:调用方法;
}
}