java 构造器的一些解释(extend相关)

类之间的关系  从底层的构造器返回到最初始的constructors

然后再从最高级往下依次进行初始化:

三个原则: 

(1) if you donnt have a superclass, the compiler gonna give you once.

(2) If u donnot have a constructor, java will give you once.

(3) 1st line in constructor must be this or super to call different constructors(a class can have mutiple constructors)

otherwise: super() : 即如果什么都没有,就自动插入这个super();

Challenge:

public class Person(){

    private String name;
    public Person (String n){
        super();
        this.name = n;
    }
    public void setName(){
    this.name = n;
    }

}


public class Student extends Person(){

    public Student(){
    this.setName("Student")
    }
}

             compiler error:

           构造器的第一行一定要调用同类或者超类的构造器,这里没有就默认super();

           回溯到超类中: 没有无参数构造器: compiler error

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值