Java- 构造方法的执行顺序

构造方法的执行过程

  • 构造方法的执行过程遵循以下步骤:
    1. 调用本类或父类的构造方法,直至最高一层(Object)
    2. 按照声明顺序执行字段的初始化赋值
    3. 执行构造函数的各语句
  • 简单的说:
    • 先父类构造,再本类成员赋值,最后执行构造方法中的语句。
class ConstructSequence 
{
    public static void main(String[] args){ 
        Person p = new Student("LiMing", 18, "PKU");
    }
}

class Person
{
    String name="unnamed";  //step 2
    int age=-1;
    Person( String name, int age ){
        super(); //step 1
        //step 3
        System.out.println( "start to create the constructor Person(),and now this.name="+this.name+",this.age="+this.age );
        this.name=name; this.age=age; 
        System.out.println( "Person() constructed completely,and now this.name="+this.name+",this.age="+this.age );
    }
}

class Student extends Person
{
    String school="unnamed"; //step2 
    Student( String name, int age, String school ){
        super( name, age );  //step 1
        //step 3
        System.out.println( "start to create the constructor Student(),and now this.name="+this.name+",this.age="+this.age+",this.school="+this.school );
        this.school = school;
        System.out.println( "Student() constructed completely,and now this.name="+this.name+",this.age="+this.age+",this.school="+this.school );
    }
}

-----------OUTPUT-----------  
start to create the constructor Person(),and now this.name=unnamed,this.age=-1
Person() constructed completely,and now this.name=LiMing,this.age=18
start to create the constructor Student(),and now this.name=LiMing,this.age=18,this.school=unnamed
Student() constructed completely,and now this.name=LiMing,this.age=18,this.school=PKU

构造方法内部调用虚方法法

  • 即调用的不是static,final,private方法。从语法上来说是合法的,但是有时会造成事实上的不合理
    • 父类调用该虚方法时,这个虚方法会跑到子类中去
class ConstructorInvokeVirtual2{
    public static void main(String[] args) {
        Person p = new Student("LiMing", 16, "PKU");
    }
}

class Person{
    String name = "unnamed";
    int age = -1;

    Person(String name, int age){
        this.name = name;
        this.age = age;
        sayHello();
    }
    void sayHello(){
        System.out.println("A Person, name: " + name + ", age: "+ age);
    }
}

class Student extends Person{
    String school = "unnamed";

    Student(String name, int age, String school){
        super(name, age);
        this.school = school;
    }

    void sayHello(){
        System.out.println("A Student, name:" + name + ", age: "+ age + ", school: " + school);
    }
}
-----------OUTPUT-----------  
A Student, name:LiMing, age: 16, school: null
  • 在本例中,在构造函数中调用了一个动态绑定的方法sayHello(),这次,会使用该方法被覆盖的定义,即使用子类中的该方法,而这时对象尚未完全构建好,所以school还没有赋值。
  • 因此,可能的话,在构建函数中避免调用任何方法,用尽可能简单的方法使对象进入就绪状态。
  • 唯一能够安全调用的是具有final属性的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值