构造函数是Underfined

时间:2016-04-03 06:27:48

标签: java

我收到一条错误,说我的构造函数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;
}

}

2 个答案:

答案 0 :(得分:2)

正如你所说的那样

super(author,numPages);

在子类词典中,
你需要在Book类中定义另一个构造函数,如

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

解决了这个错误。

答案 1 :(得分:0)

根据你唯一的构造函数:

public Book(int code, String title, double price, int quantity, String author, int numPages) 

在主要方法中尝试:

Book book = new Book(12, "title", 34d, 56, "author", 78);

而不是:

Book book = new Book("34", 56);