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 = s; 
    System.out.println("String & int args");
  }
  Flower() {
    this("hi", 47);
    System.out.println(
      "default constructor (no args)");
  }
  void print() {
    System.out.println(
      "petalCount = " + petalCount + " s = "+ s);
  }
  public static void main(String[] args) {
    Flower x = new Flower();
    x.print();
  }
}麻烦问一下 程序执行的过程,他们的顺序什么?
其中几个this的用法,是什么意思!
谢谢

解决方案 »

  1.   

    this(参数列表),就是在类的构造函数中调用满足 参数列表的另一个构造函数来初始化,此句只能在构造函数的第一句进行
    Flower()中调 this("hi", 47); 就相当于调用Flower("hi", 47); 这种写法的目的是让重载的构造函数可以共享数据初始化过程,尽最大过程的重用代码,也是弥补没有c++中缺省构造函数的遗憾
      

  2.   

    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表示调用Flower(String ss)的构造函数,必须是第一条语句
        this.s = s;   //表示,当前传进来的s,就是当前的s,不至于和域定义的s=ss,混淆,
        System.out.println("String & int args"); 
      }   Flower() { 
        this("hi", 47);  //调用Flower(String s, int petals)的构造函数
        System.out.println("default constructor (no args)"); 
      }   void print() { 
        System.out.println( 
          "petalCount = " + petalCount + " s = "+ s); 
      }   public static void main(String[] args) { 
        Flower x = new Flower(); 
        x.print(); 
      } 

      

  3.   

    this 就代表当前类本身 后边跟.运算符可以引用这个类的属性和方法
    注意 static method中不能用this
      

  4.   

    执行顺序:
    1、Flower x = new Flower(); 
    2、 Flower() { 
        this("hi", 47); 
        System.out.println( 
          "default constructor (no args)"); 
      } 
    3、Flower(String s, int petals) { 
        this(petals); 
        this.s = s; 
        System.out.println("String & int args"); 
      } 
    4、 Flower(int petals) { 
        petalCount = petals; 
        System.out.println( 
          "Constructor w/ int arg only, petalCount= " 
          + petalCount); 
      } 
    5、x.print(); 
    6、 void print() { 
        System.out.println( 
          "petalCount = " + petalCount + " s = "+ s); 
      } 
      

  5.   

    eclipse设断点在Flower x = new Flower();上,一步一步调试就知道顺序了!
      

  6.   

    this调用构造方法
    this引用属性
      

  7.   

    同意10楼。this一般引用当前的对象属性…………
      

  8.   


    this(petals);  // this表示调用Flower(int petals)的构造函数,必须是第一条语句 
      

  9.   

    this 一般引用当前对象 放在方法中的第一句
      

  10.   

    this表示对象本身
    this()表示相应的构造函数
      

  11.   

    Flower(String s, int petals) { 
        this(petals); 
        this.s = s; //成员变量和参数名称相同,使用this加以区别
        System.out.println("String & int args"); 
      } 
    你看这个程序倒着往上看!!
      

  12.   

    提供用AspectJ跟踪出来的执行顺序参考一下:Before: staticinitialization(Flower.<clinit>)  //开始Flower类的静态块初始化,类加载时发生
    After: staticinitialization(Flower.<clinit>) //结束Flower类的静态块初始化
    Before: execution(void Flower.main(String[])) //开始执行main函数
      Before: call(Flower()) //调用Flower类的构告函数
        Before: preinitialization(Flower()) //
        After: preinitialization(Flower())
        Before: initialization(Flower()) //开始实例化
          Before: execution(Flower(int)) //开始执行构造函数Flower(int petals)
            Before: set(int Flower.petalCount) //初始化Flower类的成员变量petalCount的值为0
            After: set(int Flower.petalCount)
            Before: call(java.lang.String(String)) //构造一个String实例,即"null"
            After: call(java.lang.String(String))
            Before: set(String Flower.s) //将上面那个String实例,即"null"给s初始化
            After: set(String Flower.s)
            Before: set(int Flower.petalCount) //设置petalCount的值为47
            After: set(int Flower.petalCount)
            Before: get(PrintStream java.lang.System.out) //开始println
            After: get(PrintStream java.lang.System.out)
            Before: call(java.lang.StringBuilder(String))
            After: call(java.lang.StringBuilder(String))
            Before: get(int Flower.petalCount)
            After: get(int Flower.petalCount)
            Before: call(StringBuilder java.lang.StringBuilder.append(int))
            After: call(StringBuilder java.lang.StringBuilder.append(int))
            Before: call(String java.lang.StringBuilder.toString())
            After: call(String java.lang.StringBuilder.toString())
            Before: call(void java.io.PrintStream.println(String))
    Constructor w/ int arg only, petalCount= 47
            After: call(void java.io.PrintStream.println(String))
          After: execution(Flower(int)) //构造函数Flower(int petals)结束
          Before: execution(Flower(String, int)) //开始执行构造函数Flower(String s, int petals)
            Before: set(String Flower.s) //设置s的值为"hi"
            After: set(String Flower.s)
            Before: get(PrintStream java.lang.System.out)//开始println
            After: get(PrintStream java.lang.System.out)
            Before: call(void java.io.PrintStream.println(String))
    String & int args
            After: call(void java.io.PrintStream.println(String))
          After: execution(Flower(String, int))//构造函数Flower(String s, int petals)结束
          Before: execution(Flower()) //开始执行构造函数Flower()
            Before: get(PrintStream java.lang.System.out) //开始println
            After: get(PrintStream java.lang.System.out)
            Before: call(void java.io.PrintStream.println(String))
    default constructor (no args)
            After: call(void java.io.PrintStream.println(String))
          After: execution(Flower()) //构造函数Flower()结束
        After: initialization(Flower())
      After: call(Flower())
      Before: call(void Flower.print()) //下面是x.print();的执行过程
        Before: execution(void Flower.print())
          Before: get(PrintStream java.lang.System.out)
          After: get(PrintStream java.lang.System.out)
          Before: call(java.lang.StringBuilder(String))
          After: call(java.lang.StringBuilder(String))
          Before: get(int Flower.petalCount)
          After: get(int Flower.petalCount)
          Before: call(StringBuilder java.lang.StringBuilder.append(int))
          After: call(StringBuilder java.lang.StringBuilder.append(int))
          Before: call(StringBuilder java.lang.StringBuilder.append(String))
          After: call(StringBuilder java.lang.StringBuilder.append(String))
          Before: get(String Flower.s)
          After: get(String Flower.s)
          Before: call(StringBuilder java.lang.StringBuilder.append(String))
          After: call(StringBuilder java.lang.StringBuilder.append(String))
          Before: call(String java.lang.StringBuilder.toString())
          After: call(String java.lang.StringBuilder.toString())
          Before: call(void java.io.PrintStream.println(String))
    petalCount = 47 s = hi
          After: call(void java.io.PrintStream.println(String))
        After: execution(void Flower.print())
      After: call(void Flower.print())
    After: execution(void Flower.main(String[]))
      

  13.   

    this表示自己,如果是this()的形式就表示调用自身的构造函数了。
      

  14.   


    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 = s; 
        System.out.println("String & int args"); 
      } 
      Flower() { 
        this("hi", 47); 
        System.out.println( 
          "default constructor (no args)"); 
      } 
      void print() { 
        System.out.println( 
          "petalCount = " + petalCount + " s = "+ s); 
      } 
      public static void main(String[] args) { 
        Flower x = new Flower(); 
        x.print(); //应该是先调用无参数的吧。
      }