[求助]请指点一下!
public class Flower {private int petalCount = 0;
private 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); 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);
System.out.println(
"petalCount = " + petalCount + " s = "+ s);
}
public static void main(String[] args) {
Flower x = new Flower();
x.print();
}
}
请问该程序的调用顺序是什么?我实在看不懂他们之间的调用关系是怎么回事!
----------------解决方案--------------------------------------------------------
先构造出一个 Flower对象,然后调用这个对象的print()方法
----------------解决方案--------------------------------------------------------
该程序的运行结果
Constructor w/ int arg only, petalCount= 47
String & int args
default constructor (no args)
petalCount = 47 s = hi
为什么会是47呢而不是11?版主拜托了,我是菜菜
----------------解决方案--------------------------------------------------------
这主要是构造函数的互相调用问题
一开始是无参构造函数,然后它调用了两个参数的构造函数,两个参数的构造函数又调用了一个参数的构造函数
最后才把Flower对象构造出来了
----------------解决方案--------------------------------------------------------
public class Flower {
private int petalCount = 0;
private 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); this.s = s; // Another use of "this" //这一块运行有问题呀,this必须写在第一行.两个this该怎么办?
System.out.println("String & int args");
}
Flower() {
this("hi", 47);
System.out.println(
"default constructor (no args)");
}
void print() {
this(11);
System.out.println(
"petalCount = " + petalCount + " s = "+ s);
}
public static void main(String[] args) {
----------------解决方案--------------------------------------------------------
this(s); this.s = s; // Another use of "this" //这一块运行有问题呀,this必须写在第一行.两个this该怎么办?
当然不能这样调用,不能一个构造调用两个构造函数
因为this()要放在第一行
把this(s)改为this.s=s;就不可以了吗
----------------解决方案--------------------------------------------------------
void print() {
void print() {
this(11);
System.out.println(
"petalCount = " + petalCount + " s = "+ s);
}
this(11); 这里是调用构造函数吗?
构造函数是不能显式调用的呀?
清高手讲讲原因!
谢谢了!
----------------解决方案--------------------------------------------------------
void print() {
void print() {
this(11);
System.out.println(
"petalCount = " + petalCount + " s = "+ s);
}
你这里就不是调用构造函数了
你这样做会编译通不过的
----------------解决方案--------------------------------------------------------
void print() {
this(11);
System.out.println(
"petalCount = " + petalCount + " s = "+ s);
}
那一楼的为什么能运行出结果?
难道他的this(11);
是调用??
----------------解决方案--------------------------------------------------------
一楼的程序连编译都过不了
----------------解决方案--------------------------------------------------------