解决方案 »

  1.   

    Flower x = new Flower();会调用你的默认构造函数在你默认构造函数里面调用了含有两个参数的构造函数
    Flower() {
    this("hi", 47);
    print("default constructor (no args)");
    }
    在含有两个参数的构造函数中只调用了参数为int型的构造函数,你的String ss没有调用到,所有不会有输出。
    Flower(String s, int petals) {
    this(petals);
    //! this(s); // Can’t call two!
    this.s = s; // Another use of "this"
    print("String & int args");
    }
      

  2.   

    感谢二楼的回答,但是还是有点不明白.
    Flower(String ss) {
    print("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"
    print("String & int args");
    }
    这一句不是调用String s么?
    那么为什么第一个含int参数的构造函数能被调用?
      

  3.   

    又仔细回去看了下书, 好像发现问题所在了:
    第一个含int的构造函数能被调用是不是因为第一句就赋值了,所以后面能被调用.
    第二个含String的构造函数不被调用是不是因为第一句没有赋值,而是在第二句才赋值,所以不行?
      

  4.   

    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();         //第四步:调用方法;
    }
    }
      

  5.   

    这些都是基础,任何一本Java书都有的!可以找本书看看