Java 錯誤:Constructor call must be the first statement in a constructor


今天用學校里的黑馬程序員通Java語法

想到了:在有參構造函數中調用無參構造函數

語法是這樣的:

class Person{
    private int age;
    public Person() {
        System.out.println("wu can gou zao");
    }
    
    public Person(int a) {
        age = a;
        System.out.println("you can gou zao");
        System.out.println("age shi"+ a);
this();
} }

報錯:Constructor call must be the first statement in a constructor

即 構造函數調用必須是構造函數中的第一個語句

在構造方法中,調用另一個構造函數時,必須在第一條語句中調用。

並且不能兩個構造函數相互調用,這樣不就成遞歸了么。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM