我正在用Java編寫Volume和Book類,以幫助我更好地理解構造函數和對象 - 基本上是OOP的更廣泛方面。當我嘗試創建一個主類,我收到指出以下錯誤:實際和正式參數列表長度不同
「類卷構造卷不能被應用到給定的類型; 要求:字符串,整數,圖書[] 發現:無參數 原因:實際和正式名單長度不同 ----「
這是我到目前爲止的代碼。
首先,卷類:
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
}
}
我有Volume中的構造函數正確設置(以我的理解),但仍然收到此錯誤。任何提示或建議?先謝謝你!
您定義的參數化的構造函數爲什麼你讓你的'Volume_Main'類擴展'Volume'?刪除'擴展音量'。 –
你在主要方法中添加什麼語句來調用construtor拋出錯誤? – Kick
當我用Volume擴展它時,是不是表示我將有權訪問Volume類?在我寫過其他程序之前,這對我來說已經很好了 – user3380461