zoukankan      html  css  js  c++  java
  • 子类构造函数是否会默认调用父类的无参构造函数

    1 package test;
    2 
    3 public class TestB {
    4     
    5     public TestB() {
    6         System.out.println("TestB的无参构造函数...");
    7     }
    8 
    9 }
     1 package test;
     2 
     3 public class TestA extends TestB{
     4 
     5     public TestA() {
     6         System.out.println("TestA的无参构造函数...");
     7     }
     8     
     9     public TestA(int i) {    
    10         System.out.println("TestA的有参构造函数...");
    11     }
    12     
    13     public static void main(String[] args) {
    14         TestA a1 = new TestA();
    15         TestA a2 = new TestA(1);
    16     }
    17     
    18 }

    执行上述代码后,运行结果如下:

    从上述结果得知,在TestA的有参/无参构造函数中均默认调用了父类TestB的无参构造函数,即默认执行了super()代码

  • 相关阅读:
    Docker
    Docker
    VIM
    Python
    Python
    VIM
    Python
    其他
    Java
    Java
  • 原文地址:https://www.cnblogs.com/hanw1991/p/7492148.html
Copyright © 2011-2022 走看看