对不起初学者编码器在这里,我不擅长解释事情,但我需要帮助,并想知道为什么我不断得到这个错误无论我如何格式化或重新排列日期名称和标题。所以我只是想知道是否有人可以帮助我假设的名称日期和标题在什么顺序?我正在使用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");
我一直收到此错误没有为Book找到合适的构造函数(java.lang.String,java.lang.String,int,int,int,java.lang.String)构造函数Book.Book()不适用; (实际和正式的参数列表长度不同);构造函数Book.Book(Author,Date,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
答案 0 :(得分:0)
如果要创建Book
对象,则需要使用其中一个已定义的构造函数。您有两种选择:
public Book()
public Book(Author author, Date published, String title)
第一个创建一个包含默认作者,日期和标题的书。第二个接收它们作为参数。假设第二个是您需要的那个,您现在知道:
new Book(...)
。不多也不少。Author
类型的对象。不是字符串,而不是数字,Author
。Date
对象。String
。现在,请致电:
new Book( "James", "Joyce",2013,1,1, 2013,1,1, 2013,1,1, "ULYSSES");
在此次通话中,您传递了十二个参数!它们只是字符串和数字,而不是Author
,Date
和String
。
因此,您需要创建一个Author
对象和一个Date
对象并传递它们。例如:
Author bookAuthor = new Author(...);
Date bookDate = new Date(...);
inventory[0] = new Book( bookAuthor, bookDate, "Ulysses" );
你可以在没有额外变量的情况下做同样的事情:
inventory[0] = new Book( new Author(...), new Date(...), "Ulysses" );
现在,您应该对new Author(...)
和new Date(...)
来电应用相同的逻辑。
new ...
调用来创建所需类型的对象。