以下代码展示了 Java 中构造函数的调用顺序,其中 this() 被用来调用另一个构造函数:

public class Waiter {
  int var;

  Waiter() {
    System.out.print('Good!');
  }

  Waiter(int var) {
    this();
    System.out.print('Hello!');
  }

  public static void main(String[] args) {
    Waiter t = new Waiter(1);
  }
}

代码的运行结果是:Good!Hello!

Java 构造函数调用顺序示例

原文地址: https://www.cveoy.top/t/topic/fMVN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录