2016-10-28 67 views
2

我在java中有一個類。這個類有像波紋管我怎樣才能定義一個默認的構造函數,而我有其他的構造函數?

public Person(String first, String last) { 
    firstName = new SimpleStringProperty(first); 
    lastName = new SimpleStringProperty(last); 
    city = new SimpleStringProperty("Tehran"); 
    street = new SimpleStringProperty("Vali Asr"); 
    postalCode = new SimpleStringProperty("23456"); 
    birthday = new SimpleStringProperty("12345"); 
} 

現在我要聲明構造像波紋管

public Person() { 
    Person(null, null); 
} 

一個構造函數,但它給我的錯誤。 我該怎麼辦? 感謝

+4

你會得到什麼錯誤? –

+8

改爲使用'this(null,null)'。 – Linuslabo

回答

10

調用Person(null, null)不起作用,因爲構造函數不是方法所以它不能被稱爲像一個方法,你正在嘗試做的。 你需要做的是什麼,而調用this(null, null)爲下一調用其它構造函數:

public Person() { 
    this(null, null); // this(...) like super(...) is only allowed if it is 
         // the first instruction of your constructor's body 
} 

public Person(String first, String last) { 
    ... 
} 

更多細節有關關鍵字thishere

+2

請注意,調用'this(...)'或'super(...)'必須始終是構造函數中的第一條指令。這也意味着你可以調用'this(...)'或'super(...)',但不能同時調用這兩個。 – Turing85

+1

公衆人物(第一字符串,字符串最後一個){// 設置支桿 的} 公衆人物(){ 這個(NULL,NULL); } –

相關問題