There are several syntax errors in your code. Here's the corrected version:

public class Waiter {
    int var;

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

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

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

Explanation:

  • Constructor Chaining: The Waiter(int var) constructor uses this() to call the default constructor Waiter(), ensuring the 'Good!' message is printed before 'Hello!' in the output.
  • Syntax Corrections: The code has been corrected for syntax errors like missing semicolons, incorrect method names, and incorrect capitalization.
Java Waiter Class with Constructor Chaining: Corrected Code Example

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

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