Java初始化构造函数的顺序

先祖先,再客人,最后自己

class Person{
    public Person(){
        System.out.println("Person");
    }
}

class School{
    public School(){
        System.out.println("School");
    }
}

class Student extends Person{
    School school = new School();
    
    public Student(){
        System.out.println("Student");
    }
}


Student student = new Student();
//执行顺序为Person>School>Student

Person
School
Student

猜你喜欢

转载自www.cnblogs.com/fiwen/p/9005371.html