java基础

   今天看了下关于java构造函数的内容,虽然现在对JVM的工作原理之类的了解不是很深,下面是我看的别人的代码,别人总结出来的,有点同感,继续深入了解下去:

public class Son extends Father {
    SonProp r = new SonProp();
    public Son() {
        System.out.println("Son is construct");
    }
    public static void main(String[] args) {
        new Son();
    }
}
class Father {
    FatherProp SonProp = new FatherProp();
    public Father() {
        System.out.println("Father is construct");
    }
}
class SonProp {
    public SonProp() {
        System.out.println("SonProp is construct");
    }
}
class FatherProp {
    public FatherProp() {
        System.out.println("FatherProp is construct");
    }
}执行结果如下:
FatherProp is construct
Father is construct
SonProp is construct
Son is construct

 

几点总结:
(1)对象是由new运算符创建的,且在任何构造函数执行之前就已经创建完毕了
(2)构造函数的执行总是“向上”的:而且总是先执行完父类的构造函数
(3)在构造函数中,没有this()语句则由super()语句。没有this()时,或者自己编写super(),或者由系统自动调
    用 super()  
(4)显式初始化语句总是先于构造函数语句,但后于super()或this()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值