- 类(对象)中没有构造器时,是默认会自动创建一个无参构造器的;
- 当类(对象)中只有有参构造器时,就不会再自动创建一个无参构造器了;
- 当子类的构造器中没有实现super,默认在调用子类构造方法之间先调用父类的无参构造方法。
-
子类构造器会默认调用super()(无论构造器中是否写有super()),用于初始化父类成员,同时当父类中存在有参构造器时,必须提供无参构造器,子类构造器中并不会自动继
public class Super { Super(){ printThree(); } private void printThree() { System.out.println("Three"); } static class Test extends Super{ int three= (int) Math.PI; private void printThree() { System.out.println(three); } } public static void main(String[] args) { Test test = new Test(); test.printThree(); } }
承有参构造器,仍然默认调用super(),使用无参构造器。
父类,子类 ,构造方法的执行顺序
最新推荐文章于 2025-03-26 20:54:54 发布