我有一个项目,希望我创建3个类:TextBook,LibraryCard和Library,我需要将TextBook Objects放入书架数组中;我已经创建了前2个,并且它们工作正常,但是,库类给了我这个错误:
Constructor TextBook in class TextBook cannot be applied to given types;
Required: java.lang.string
Found: no arguments
reason: actual and formal argument lists differ in length
这是教科书类的代码:
public class TextBook
{
private String Title;
private int Chapters;
private int LastChapter;
/**
* Constructor for objects of class TextBook
*/
public TextBook(String Titlename, int Chapters_init)
{
// initialise instance variables
Title = Titlename;
Chapters = Chapters_init;
LastChapter = 0;
}
public String getTitle()
{
return Title;
}
public void readNextChapter()
{
for(LastChapter=0;LastChapter<Chapters;LastChapter++) {
System.out.println("You have finished the book!");
}
}
public boolean isfinished()
{
if (LastChapter == Chapters)
return true;
else
return false;
}
public void closeBook()
{
LastChapter = 0;
}
public void describe()
{
System.out.println("You are currently Reading " + Title);
}
这是库类的代码:
public class Library
{
int nextBook;
int borrowers = 0;
public Library(int Max_Books)
{
TextBook[] bookshelf = new TextBook[5];
for(int ptr=0;ptr<bookshelf.length;ptr++)
bookshelf[ptr] = new TextBook();
}
先谢谢您。
答案 0 :(得分:1)
您试图通过调用默认构造函数来创建TextBook的实例。
在您的类中,您定义了一个构造函数,该构造函数的签名需要一个String和一个int。
默认构造函数为:
public TextBook() {
// body
}
因此,您可以将此上面的构造函数添加到代码中,或者通过向其提供必需的参数来正确使用现有的构造函数。