gpt4 book ai didi

java - 构造函数未定义

转载 作者:行者123 更新时间:2023-11-30 02:59:13 24 4
gpt4 key购买 nike

我收到一条错误消息,说我的构造函数 Book(String, int) 在我的 Dictionary 类中未定义。我检查了其他所有内容并尝试重做我的程序仍然有错误。有解决这个问题的解决方案吗?

图书类别:

public class Book{
private String author;
private int numPages;

public Book(int code, String title, double price, int quantity, String author, int numPages){
this.author = author;
this.numPages = numPages;
}

//Getters
public String getAuthor(){
return this.author;
}
public int getNumPages(){
return this.numPages;
}

//Setters
public void setAuthor(String author){
this.author = author;
}
public void setNumPages(int numPages){
this.numPages = numPages;
}

//toString
@Override
public String toString(){
String outputBookDes = "";
outputBookDes += "Author: "+ this.author;
outputBookDes += "Number Pages: "+ this.numPages;
return outputBookDes;
}
}

字典类:

public class Dictionary extends Book{
private String language;
private int numDefinitions;

public Dictionary(String author, int numPages, String language, int numDefinitions){
super(author,numPages);
this.language = language;
this.numDefinitions = numDefinitions;
}

// Getters
public String getLanguage(){
return this.language;
}
public int getNumDefinitions(){
return this.numDefinitions;
}

//Setters
public void setLanguage(String language){
this.language = language;
}
public void setNumDefinitions(int numDefinitons){
this.numDefinitions = numDefinitions;
}

//Ratio method
public double getRatio(){
double ratioTotal = this.numDefinitions / getNumPages();
return ratioTotal;
}

}

最佳答案

当你打电话时

super(author,numPages);

在子类字典中,
您需要在 Book 类中定义另一个构造函数,例如

public Book(String author,int numPages) {
this.author = author;
this.numPages = numPages;
}

解决此错误。

关于java - 构造函数未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36382291/

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