public class Person7 { String name; int age; public Person7(){ System.out.println("1.public Person7(){}" ); } } class Studentt extends Person7{ String school; public Studentt() { System.out.println("2.public Student(){}"); } }
/** * Created by xabcd on 2019/2/15. */ public class TestPerson7Student { public static void main(String[] args) { Studentt s = new Studentt(); } }
结果: 1.public Person7(){} 2.public Student(){} 自类在实例化时先调用父类中的无参构造方法再调用有参构造方法。 可以改在一个文件中:
/** * Created by xabcd on 2019/2/15. */ public class Person7 { String name; int age; public Person7(){ System.out.println("1.public Person7(){}" ); } } class Studentt extends Person7{ String school; public Studentt() { System.out.println("2.public Student(){}"); } public static void main(String[] args) { Studentt s = new Studentt(); } }