2016-10-28 93 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); } –

相关问题