错误:Book不是抽象的,并且不会覆盖Citable中的抽象方法getPageCount。我应该根据接口类创建一个类摘要吗?

时间:2017-08-18 12:18:54

标签: java interface abstract-class comparable

我正在尝试做一个学校项目,我正在编写一个处理书籍,书籍集合(我们称之为图书馆)的应用程序,以及这些类的UI客户端。

其中一个类是一个名为Citable的类,它需要实现典型的访问器功能,这些功能可以返回书籍的ISBN,作者,标题和发布者(所有字符串)以及发布年份,月份和日期(所有整数) )和页数(整数)。

下一个类是实现Citable接口的Book类。图书馆需要按照他们的ISBN号对书籍进行排序,所以在Book中你也需要提供对它的支持(提示:这需要实现一个额外的界面(你不必编写,只需实现)和写一个额外的方法(应该只需要一行代码),并且该库进行简单的调用以使排序发生)。这里没有静态(测试方法除外),也没有任何UI。我会告诉你到目前为止我所做的事情:

public interface Citable
{
    public String getISBN();
    public String getAuthor();
    public String getTitle();
    public String getPublisher();
    public int getPublicationYear();
    public int getMonth();
    public int getDay();
    public int getPageCount();
}

和书界面:

import java.util.*;
public abstract class Book implements Citable, Comparable
{
    /**
     * Constructor for objects of class Book
     */
    public Book()
    {
        Collections.sort(ISBN);
    }

    public String toString (){
        String info =  ISBN + "\n" + title + "\n" + author + "\n" + publisher + 
        "\n" + publicationYear + "\n" + month + "\n" + day;
        return info;
    }
}

在Book课程中,我实现了我制作的界面和Java内置的Comparable。我的问题是:在Book类中,我收到错误:Book不是抽象的,并且不会覆盖Citable中的抽象方法getPageCount。我可以阻止错误的唯一方法是使Book类抽象化。但是,我没有表明我应该将该课程抽象化。有没有办法解决这个问题?

2 个答案:

答案 0 :(得分:0)

您的班级Book实现了两个界面,即CitableComparable。这意味着您必须从类中的这些接口实现方法。唯一的另一种方法是使类Book抽象,这意味着你无法实例化它。但这绝对不是你想要的。

您的班级Book看起来像这样:

public class Book implements Citable, Comparable
{
    /**
     * Constructor for objects of class Book
     */
    public Book()
    {
        Collections.sort(ISBN);
    }

    public String toString (){
        String info =  ISBN + "\n" + title + "\n" + author + "\n" + publisher + 
        "\n" + publicationYear + "\n" + month + "\n" + day;
        return info;
    }

    @Override
    public int compareTo(Object o) {
        //Implement this method
    }

    @Override
    public String getISBN() {
        return isbn;
    }

    //other methods from your interfaces here...
}

答案 1 :(得分:0)

据我所知,您的解决方案策略是为方法契约创建一个接口,并在您的类中实现此接口。 为此,首先让我提醒一下,一旦实现了接口,就需要实现它的所有抽象方法。错误与它有关。因此,有两个选项,首先,您可以在Book类中实现Citable方法。其次,您可以在Citable接口中将这些方法编写为默认方法,并在Book类中使用它们。 我已经与一些评论共享了示例代码,请检查它们

示例代码:

import java.util.Collections;

public  class Book implements Citable, Comparable
{
    /**
     * Constructor for objects of class Book
     */
    public Book()
    {
        Collections.sort(getISBN());//there is no sort method for Strings, sort method is only for lists
    }

    public String toString (){
        String info =  getISBN() + "\n" + title + "\n" + author + "\n" + publisher + 
        "\n" + publicationYear + "\n" + month + "\n" + day;//You need to use getter methods
        return info;
    }

    @Override
    public int compareTo(Object o) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public String getISBN() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getAuthor() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getTitle() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getPublisher() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int getPublicationYear() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int getMonth() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int getDay() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int getPageCount() {
        // TODO Auto-generated method stub
        return 0;
    }
}
相关问题