62,614
社区成员
发帖
与我相关
我的任务
分享
public class Flower
{
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(); //第四步:调用方法;
}
}