gpt4 book ai didi

java - 实际和形式参数列表的长度不同

转载 作者:行者123 更新时间:2023-11-30 09:11:16 25 4
gpt4 key购买 nike

我当时正在用 Java 编写一个 Volume 和 Book 类,以帮助我更好地理解构造函数和对象——基本上是 OOP 的更广泛的方面。当我尝试创建一个主类时,我收到一条错误消息,内容如下:

“类 Volume 中的构造函数 Volume 不能应用于给定类型; 必需:字符串、整数、书籍[] 发现:没有参数 原因:实际列表和正式列表的长度不同----”

这是我目前的代码。

首先是Volume类:

public class Volume extends Book{

public String volumeName;
public int numberOfBooks;
public Book[] books;

// Constructor with parameters
public Volume(String volumeName,int numberOfBooks,Book[] books){
this.volumeName = volumeName;
this.numberOfBooks = numberOfBooks;
this.books = new Book[numberOfBooks];
}

// String representation of the Volume
public static String toString(Volume volume){
String volumeDescription = "Here are the details of the selected Volume:\n";
volumeDescription += "The volume's name is \"" + volume.volumeName + "\".\n";
volumeDescription += "It contains " + volume.numberOfBooks + " books.\n";
volumeDescription += "Here is a list of books it contains:\n";
for(int i = 0; i < volume.numberOfBooks; i++){
volumeDescription += "[" + i + "] " + volume.books[i];
}
return volumeDescription;
}

// Description of each book in the Volume
public static String getBookArray(Volume volume){
Book[] listOfBooks = volume.books;
String bookDescriptions = "Here is a description of each book in the Volume.\n";
for(int i = 0; i < listOfBooks.length; i++){
bookDescriptions += "Book #" + i + ":\n";
bookDescriptions += "Title: " + listOfBooks[i].title + "\n";
bookDescriptions += "Author: " + listOfBooks[i].author + "\n";
bookDescriptions += "Number of pages: " + listOfBooks[i].numberOfPages + "\n";
}
return bookDescriptions;
}

这是主要的,我收到上面提到的错误:

public class Volume_Main extends Volume{

public static void main(String[] args) {
// TODO code application logic here
}

我已正确设置卷中的构造函数(据我所知),但仍然收到此错误。有什么提示或建议吗?提前致谢!

最佳答案

在 Java 中,每个类都必须有一个构造函数。如果您没有明确定义,编译器实际上会为您插入一个空的构造函数。如果您自己没有显式调用父类(super class)构造函数,构造函数也会隐式调用父类的无参数构造函数。因此,您的 Volume_Main 类具有以下构造函数:

public Volume_Main() {
super()
}

但是,Volume_Main 的父类是Volume,它没有无参构造函数,因此编译器会抛出错误。 Volume_Main 可能没有理由扩展 Volume,但至少您必须在 Volume 中明确定义一个无参数类.

关于java - 实际和形式参数列表的长度不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22180615/

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