java的一个小问题,高手过来指点......
//Calling constructors with "this" 用“this”调用构造函数public class Flower
{
int petalCount = 0;
String s = "initial value";
Flower(int prtals)
{
petalCount = prtals;
System.out.println("Constructor w/ String arg only, prtalcount = " + 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); //Can't call two! //不能叫两个!
this.s = s; //Another use of "this" // “this”的另一个用法
System.out.println("String & int args"); //字符串和字符串
}
Flower()
{
this("hi", 47);
System.out.println("default constructor (no args)"); //缺省构造函数(无ARG)
}
void printPetalCount()
{
//! this(11); //Not inside non - constructor! 不在非构造函数之内!
System.out.println("petalCount = " + petalCount + " s = " + s);
}
public static void main(String[] args)
{
Flower x = new Flower();
x.printPetalCount();
}
}