java一个简单程序进来帮看下

来源:百度知道 编辑:UC知道 时间:2024/06/16 11:54:47
public class Flower {
int petalCount = 0;
String s = new String("null");
Flower(int petals){
petalCount = petals;
System.out.println("11111111"+petalCount);
}
Flower(String ss){
System.out.println("22222222"+ ss);
s = ss;
}
Flower(String s,int petals){
this(petals);
this.s=s;
System.out.println("string&&int");
}
Flower(){
this("hi",47);
System.out.println("qqqq(no args)");
}
void print(){
System.out.println("petalCount "+ petalCount +" s "+ s);
}
public static void main(String[] args) {
// TODO Auto-generated method stub

Flower f = new Flower();
f.print();
}

}
请问这段程序中petalCount = petals; 都被付值成47,怎么把47给他们的?清明白的讲解下谢谢了!本人刚学JAVA不台懂
petals在什么时候被付值成47?

你在main()函数中调用了没有参数的构造函数.就是:
Flower(){
this("hi",47);
System.out.println("qqqq(no args)");
}
其中:this("hi",47); 有调用了!带两个参数的构造函数!
Flower(String s,int petals){
this(petals);
this.s=s;
System.out.println("string&&int");
}
其中:this(petals); 有调用带一个参数的构造函数:
Flower(int petals){
petalCount = petals;
System.out.println("11111111"+petalCount);
}
其中:petalCount = petals;就把值付给petalCount了,是47!

Flower(){
this("hi",47);
System.out.println("qqqq(no args)");
}
在这一句赋值的.这个是你main函数里面 new Flower() ;
会调用这个构造函数,然后这个构造函数里面调用了Flower(String s,int petals)这个构造函数,实现了赋值操作.