java中构造方法的执行过程_关于java中构造方法、实例初始化执行顺序

父类Person

package Person_exdends;

class Person {

String name = "未命名";

int age = -1;

Person(String name, int age) {

super();

System.out.println("开始构造方法person(),此时this.name=" + this.name + " this.age=" + this.age);

this.name = name;

this.age = age;

System.out.println("person()构造完成,此时this.name=" + this.name + " this.age=" + this.age);

}

}

子类Student

package Person_exdends;

class Student extends Person {

String school = "未定义学校";

Student(String name, int age, String school) {

super(name, age);

System.out.println(

"开始构造方法Student(),此时this.name=" + this.name + " this.age=" + this.age + " this.school=" + this.school);

this.school = school;

System.out.println(

"Student()构造完成,此时this.name=" + this.name + " this.age=" + this.age + " this.school=" + this.school);

}

}

package Person_exdends;

public class Main {

public static void main(String[] args) {

new Student("Tom", 20, "清华");

}

}

014bb906d62b04fd26467e52cde0db58.png

b5704b500f0af8c65406cd960c1fa789.png

1e04150ae99243fd35a15bb6c361ea3a.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值