/** * 测试构造器类 * 打印花瓣数量 * <p> * 很喜欢《Thing in java》5.4章中关于this关键字的这个小例子 * 写下来仅表纪念和喜欢 * </P> * @author zhangss 2017-3-27 17:12:10 * @version 1.0 * */ 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/ int arg only, s = " + ss); } Flower(String s, int petals){ this(petals); // this(petals);// can't call two! this.s = s; System.out.println("String & int args"); } Flower(){ this("hi", 47); System.out.println("default constructor (no args)"); } void printPetalCount(){ // this(11); // not inside non-constructor System.out.println("petalCount = " + petalCount + " s " + s); } public static void main(String[] args){ Flower f = new Flower(); f.printPetalCount(); } }