【发布时间】:2016-03-02 01:27:32
【问题描述】:
嗨,对不起,这里是初学者,我不擅长解释事情,但我需要帮助,想知道为什么无论我如何格式化或重新排列日期名称和标题,我都会收到此错误。所以我只是想知道是否有人可以帮助我按照什么顺序输入姓名日期和标题?我正在使用 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
【问题讨论】:
-
当您调用
new Book( "James", "Joyce",2013,1,1, 2013,1,1, 2013,1,1, "ULYSSES");时,您尝试调用哪个构造函数? -
我假设它来自 Book 构造函数,对不起,就像我说我是初学者编码器,我不太确定这就是为什么我发布尽可能多的信息,以便我希望能够制作人们更容易回答我的问题。谢谢@Pshemo
-
初学者没问题,不用担心。但试着回答我之前的问题。当您调用
new SomeClass(arguments)时,new创建SomeClass的对象,然后从SomeClass(arguments)调用代码以正确初始化它(设置其字段,或执行您在构造函数中描述的其他一些其他事情)。但是由于SomeClass中可以有多个构造函数,基于作为arguments传递的数据类型,它需要决定使用哪个构造函数。因此,如果您通过new SomeClass("Jack", 20),它将尝试找到SomeClass(String, int)而不是SomeClass(Person)。 -
那么,当你调用
new Book( "James", "Joyce",2013,1,1, 2013,1,1, 2013,1,1, "ULYSSES")时,应该从Book类中的哪个构造函数中执行代码?
标签: java class constructor bluej