public class Bootchy {
   int bootch;
   String snootch;
  public Bootchy(){
this("snootchy");
System.out.println("first");
  }
  
  public Bootchy(String snootch){
  this(420,"snootchy");
  System.out.println("second");
  }
  
  public Bootchy(int bootch,String snootch){
  this.bootch=bootch;
  this.snootch=snootch;
  System.out.println("third");
  }
  
  public static void main(String args[]){
Bootchy b=new Bootchy();
System.out.println(b.snootch+" "+b.bootch);
  }
}What is the result?  
A. snootchy 420 third second first 
B. snootchy 420 first second third 
C. first second third snootchy 420 
D. third second first snootchy 420 
E. thirds first second snootchy 420 
F. first second first third snootchy 420
Answer: ( D )
参考大纲:面向对象 — 覆载
不明白请高手讲解!

解决方案 »

  1.   


    这个容易啊,
    public static void main(String args[]){
    Bootchy b=new Bootchy();//这一句会调用空构造函数,即Bootchy()-->Bootchy(String str)-->Bootchy(int bootch,String snootch),依次打印的就是third second first 
    System.out.println(b.snootch+" "+b.bootch);//这一句就打印通过上一个方法设定的值啊
      }
    }
      

  2.   

    main的new Bootchy()会进入1
    1 public Bootchy()的this("snootchy")会调用Bootchy(String snootch)
    2 Bootchy(String snootch)的this(420,"snootchy")会调用Bootchy(int bootch,String snootch)
    3 Bootchy(int bootch,String snootch)先赋值,然后打印third
    然后3调用结束返回到2,2接着打印second
    然后2调用结束返回到1,1接着打印first
    然后1调用结束返回main
    然后main继续打印在3中赋了值的snootch和bootch
      

  3.   

    我想知道这里为什么用this,用super不行吗
      

  4.   

    super和this都是指向对象的引用,但是它是指向父类对象的引用