gpt4 book ai didi

java - 类 TextBook 中的构造函数 TextBook 无法应用于给定类型

转载 作者:行者123 更新时间:2023-12-02 09:18:58 25 4
gpt4 key购买 nike

我有一个项目希望我创建 3 个类:TextBook、LibraryCard 和 Library,并且我需要将 TextBook 对象放入书架数组中;我已经创建了前 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();
}

提前谢谢您。

最佳答案

您正在尝试通过调用默认构造函数来创建 TextBook 的实例。

在您的类中,您定义了一个构造函数,其签名需要一个 String 和一个 int。

默认构造函数是:

public TextBook() {
// body
}

因此,您可以将上面的构造函数添加到您的代码中,或者通过为其提供所需的参数来正确使用现有的构造函数。

关于java - 类 TextBook 中的构造函数 TextBook 无法应用于给定类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58827175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com