java基础问题

悬赏:3 发布时间:2008-07-21 提问人:dixuexiongying (初级程序员)

public class Flower {
  private int petalCount = 0;
  private String s = new String("null");
  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);
//!    this(s); // Can't call two!
    this.s = s; // Another use of "this"
    System.out.println("String & int args");
  }
  Flower() {
    this("hi", 47);
    System.out.println(
      "default constructor (no args)");
  }
  void print() {
//!    this(11); // Not inside non-constructor!
    System.out.println(
      "petalCount = " + petalCount + " s = "+ s);
  }
  public static void main(String[] args) {
    Flower x = new Flower();
    x.print();
  }
} ///:~

   请问一下上面的输出结果是什么,为什么!说明原因

采纳的答案

2008-07-22 lggege (架构师)

我初看还以为是类的变量的初始化顺序呢.

这个, 不就是一层一层的调用this上去么. 再一层一层的回来. 没特别的地方呀.
输出顺序等于调用顺序, 再加上 调用结束回来继续向下的输出.

Constructor w/ int arg only, petalCount= 47
// this(petals);

String & int args
// this("hi", 47);

default constructor (no args)
// new Flower();

petalCount = 47 s = hi
// x.print();

提问者对于答案的评价:
谢谢