對不起,初學者編碼器在這裏,我不擅長解釋的東西,但我需要幫助,並想知道爲什麼我不斷得到這個錯誤,無論我如何格式化或重新排列日期名稱和標題。所以我只是想知道是否有人可以幫助我以什麼順序,我把名稱日期和標題放在?我正在使用BlueJ編譯器。找不到適合本書錯誤的構造函數?
這裏是我的,我有問題的代碼:
public BookStore()
{
inventory = new Book[100];
inventory[0] = new Book("James", "Joyce",2013,1,1, 2013,1,1, 2013,1,1, "ULYSSES");
inventory[1] = new Book(2013, "THE GREAT GATSBY", "F. Scott Fitzgerald");
我不斷收到此錯誤發現書沒有合適的構造(java.lang.String中,java.lang.String中,INT,INT ,int,java.lang.String)構造函數Book.Book()不適用; (實際和正式參數列表的長度不同);構造Book.Book(作者,日期,java.lang.String中)不適用(實際的和正式的參數列表的長度不同)
這裏是Book類:
private static final String DEFAULT_TITLE = "Untitled";
private Author author;
private Date published;
private String title;
public Book()
{
this.author = new Author();
this.published = new Date();
this.title = DEFAULT_TITLE;
} // end constructor
public Book(Author author, Date published, String title)
{
setAuthor(author);
setDatePublished(published);
setTitle(title);
} // end constructor
public Author getAuthor()
{
return author;
} // end accessor
public Date getDatePublished()
{
return published;
} // end accessor
public String getTitle()
{
return title;
} // end accessor
public void setAuthor(Author author)
{
this.author = (null == author ? new Author() : author);
} // end accessor
public void setDatePublished(Date published)
{
this.published = (null == published ? new Date() : published);
} // end accessor
public void setTitle(String title)
{
this.title = (null == title ? DEFAULT_TITLE : title);
} // end accessor
public String getAuthorName()
{
return author.getName().getFullName();
} // end method
public String getDayOfTheWeekBookWasPublished()
{
return published.getDayOfTheWeek();
} // end method
public void printDetails()
{
System.out.print(getAuthorName());
System.out.print("(");
System.out.print(author.getName().getInitials());
System.out.print(") wrote ");
System.out.print(title);
System.out.print(" on ");
System.out.print(getDayOfTheWeekBookWasPublished());
System.out.print(", ");
System.out.print(Date.getMonthName(published.getMonth()));
System.out.print(" ");
System.out.print(published.getDay());
System.out.print(", ");
System.out.print(published.getYear());
Name pseudonym = author.getPseudonym();
if (null != pseudonym)
{
System.out.print(", under the pseudonym ");
System.out.print(pseudonym.getFullName());
}
System.out.println();
} // end method
} // end class
當你調用'new Book(「James」,「Joyce」,2013,1,1,2013,1,1,2013,1,1,「ULYSSES」)時,你試圖調用哪個構造函數;'? – Pshemo
我假設它來自Book構造函數,對不起,就像我說過我是初學者編碼器,我不太確定那就是爲什麼我發佈了儘可能多的信息,因爲我希望可以讓人們更容易回答我的問題題。謝謝@Pshemo – Tre
初學者可以,所以不用擔心。但試着回答我以前的問題。當你調用'New SomeClass(arguments)'時,'new'創建'SomeClass'的對象,然後調用'SomeClass(arguments)'中的代碼來正確初始化它(設置它的字段,或者做一些其他你在構造函數)。但是由於'SomeClass'中可以有多個基於作爲'arguments'傳遞的數據類型的構造函數,因此需要決定使用哪個構造函數。所以如果你傳遞'SomeClass(「Jack」,20)''它會試着找到'SomeClass(String,int)'而不是'SomeClass(Person)'。 – Pshemo