import java.io.*;
class people{
String name;
int age;
people(){
System.out.println("In people1");
}
people(String name,int age){
this.name=name;
this.age=age;
System.out.println("In people2");
}
}
class student extends people{
String school;
student(){
//super();
this(null,0,null);
System.out.println("In student1");

}
student(String name,int age,String school){
super(name,age);
this.school=school;
System.out.println("In student2");
}
}
class gradute extends student{
String time;
gradute(){
this("a");
System.out.println("In gradute1");
}
gradute(String s){
this.time=s;
System.out.println("In gradute2");
}
}
public class Exam3_16{
public static void main(String args[]){
gradute d=new gradute();
}

}请问以上程序的输出中为何没有"In people1"?
student(){
//super();
this(null,0,null);
System.out.println("In student1");
}
此构造方法不是隐含调用super()么?应该去调用people()才对!

解决方案 »

  1.   

    student(){
    //super();
    this(null,0,null);
    System.out.println("In student1");
    }如果你这里没有this(null,0,null);这句,那么就象你说的:
    构造方法不是隐含调用super();
    但是,由于你自己显式的调用了this(null,0,null);
    而在student(String name,int age,String school){
    super(name,age);
    里,又调用了super(name,age);
    所以,就有了你看到的结果。不知道这样解释清楚吗?
      

  2.   

    但是为何在
    gradute(){}中使用了this("a");却依然调用student();
      

  3.   

    java里是这样的:
    当你有显式的调用父类的构造函数时,就按你的执行;
    当没有显式的调用时,就默认调用父类的没有参数的构造函数。所以,如果你的父类里没有   没有参数的构造函数,而你的子类又不显式的指明调用父类的某一个构造函数,编译就不能通过!!!
    不知道明白了没?
      

  4.   

    虚拟机要保证在你没有强制调用同一个类的一个以上构造函数时,只能调用一个构造函数
    如果你强制调用了一个构造函数,就不可能自动调用另一个
    这里你强制调用了people(String name,int age),所以people()就不被调用
    而student的任何一个构造函数你都没有强制调用,所以默认调用student()构造函数
      

  5.   

    给个实例:
    参考上面解释,如果去掉以下这个构造函数,
    student(){
    //super();
    this(null,0,null);
    System.out.println("In student1");

    }
    那么,gradute 这个类将编译不能通过!!!
      

  6.   

    因为student中是 super(name, age);,所以就调用
    People(String name, int age) {
    this.name = name;
    this.age = age;
    System.out.println("In people2");
    }
    如果改成
    Student(String school) {
    this.school = school;
    System.out.println("In student2");
    }Student() 
    {
    this(null);
    System.out.println("In student1");
    }就会显示In people1了
      

  7.   

    同意 yeah920 的观点没有显示调用构造函数的时候才会默认调用父类的构造函数.
      

  8.   

    people(String name,int age){
             this();
            this.name=name;
            this.age=age;        System.out.println("this is in people2");
    这样写people的带参构造函数,就有" in people1"了.
      

  9.   

    import java.io.*;
    class people{
    String name;
    int age;
    people(){
    System.out.println("In people1");
    }
    people(String name,int age){
    this.name=name;
    this.age=age;
    System.out.println("In people2");
    }
    }
    class student extends people{
    String school;
    student(){
    //super();
    this(null,0,null);
    System.out.println("In student1");

    }
    student(String name,int age,String school){
    //super(name,age);
    this.school=school;
    System.out.println("In student2");
    }
    }
    class gradute extends student{
    String time;
    int n;
    String com;
    gradute(){
    this("a",4,"hua wei");
    System.out.println("In gradute1");
    }
    gradute(String s,int d,String v){
    super(s,d,v);
    this.time=s;
    this.n=d;
    this.com=v;
    System.out.println("In gradute2");
    }
    }
    public class Exam3_16{
    public static void main(String args[]){
    gradute d=new gradute();
    }

    }