super 与 this 同时使用问题

你们都知道this 和 super 调用构造函数时都必须放在第一句,今天同窗问个人一个问题有点意思。函数

那么:我怎么在子类中 显式的用 super 初始化父类同时用 this 初始化子类?this

-------------------------------------------------------------------------------------spa

首先你们必须认识到两点:code

1. super 调用构造函数的 做用是 为了初始化子类前先初始化父类,仅此而已。blog

2. 每一个类的构造函数有且仅有一个会执行属性的初始化,即便你用的this, 那最后实际上也是this 指向的那个构造函数会执行。编译

 

下面分两种状况具体分析下class

(1) 父类提供了无参的构造函数构造函数

这种状况比较简单,由于子类的构造函数会自动的调用父类的无参构造方法,不须要显式的加 super()。方法

但若是我手贱,硬要加怎么办?那么会没法编译,否则这样就屡次初始化父类了,有 致使不一致状态 的潜在风险。总结

以下图:父类提供了一个无参的构造函数,子类有一个默认构造函数,三个带参的构造函数。

    上文说了,实例化时,有且仅有一个构造函数会执行,当用son1(), son1(int p3) 都最终会调用最后一个构造函数,

    因此最后一个构造函数执行前会调用父类的构造函数,若是son1(), son1(int p3) 也显示的调用super(),则屡次初始化父类了,

    这显然是不对的;而 son1(int p2, int p3) 本身初始化属性,因此没这个问题,能够显式的调用super()。

 

public class father {
     int age;
    public father(){
        age = 50;
    }
}

public class son1 extends father{
    int property1;
    int property2;
    int property3;
    
    public son1(){
        this(1,2,3);
    }
    
    public son1(int p3){
        this(1,2,p3);
    }
    
    public son1(int p2, int p3){
        super();
        property1 = 1;
        this.property2 = p2;
        this.property3 = p3;
    }
    
    public son1(int p1, int p2, int p3){
        super();
        property1 = p1;
        property2 = p2;
        property3 = p3;
    }
}

 

(2) 父类没有提供无参的构造函数

此时必须在子类中显式调用super(parm) 初始化父类。

一样,后面的两个构造函数必须显式的调用 super

public class Father {

    int age;

    public Father(int age) {
        this.age = age;
    }
}


public class Son extends Father{
   int property1;
   int property2;
   int property3;
   
//构造函数1
   public Son(){
       //super(40);
       this(1,2,3);
   }
   
//构造函数2
   public Son(int p3){
       //super(40);
       this(1,2,p3);
   }
  
//构造函数3
   public Son(int p2, int p3){
       super(40);
       property1 = 1;
       this.property2 = p2;
       this.property3 = p3;
   }
  
//构造函数4
   public Son(int p1, int p2, int p3){
       super(40);
       property1 = p1;
       property2 = p2;
       property3 = p3;
   }
} 

 

总结:this 与  super 调用构造函数时,都必须第一行,这样致使他们不能同时使用,但你并不须要担忧没有初始化父类。

   由于,this 最终指向的子类构造函数中,必定会调用super() 初始化父类,默认的或者带参的。

相关文章
相关标签/搜索