下面这个程序的输出总是
father
child
为什么有没有super()调用,结果都一样呢?
那么ctor里面第一句话写上super()又有什么意义呢?
---------------------------------------------------
public class j001 {
public static class f{
public f(){
System.out.println("father");
}
}
public static class c extends f{
public c(){
//super();  有没有这句话似乎都一样。
System.out.println("child");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
@SuppressWarnings("unused")
j001.c c1=new j001.c();
}
}

解决方案 »

  1.   

    默认的是 super()
    如果是需要用的是父类的有参构造就必须指定了
      

  2.   

    子类的构造函数的调用必须先完成父类的调用,这是系统初始化特性,所以即使没有super 也会先调用父类的构造函数啊,super()在这里只是强调作用,让大家看的明明白白
      

  3.   

    楼主不妨重载一下父类的构造器,加个参数,然后在子类中super下,看看结果有什么区别
      

  4.   

    class people{
    String name;
    String sex;
    people(String n,String s){
    name = n;
    sex = s;
    System.out.println(this.toString());
    }
    }class male extends people{
    int age;
    male(String n, String s) {
    super(n, s);
    sex = "男";
    // TODO Auto-generated constructor stub
    System.out.println(this.toString());
    System.out.println(super.toString());
    }}class female extends people{
    int age;
    female(String n, String s,int i) {
    super(n, s);
    sex = "女";
    age = i;
    // TODO Auto-generated constructor stub
    System.out.println(this.toString());
    System.out.println(super.toString());
    }}public class extends1 { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub male m1;
    m1 = new male("张三","女");

    female f1;
    f1 = new female("小龙女","男",20);

    System.out.println(f1.age+" "+f1.name+" "+f1.sex);
    System.out.println(m1.name+" "+m1.sex);
    }}
    你试验下这个