java - 该类型必须实现继承的抽象方法

标签 java inheritance interface

我正在开发一个中级 Java 程序,我觉得我的逻辑是正确的,但是我在类介绍中遇到了这个错误:

The type QuestionSet must implement the inherited abstract method IQuestionSet.add(IQuestion)

我确实有这个方法,但是我有一个问题对象作为参数而不是 IQuestion(问题的接口(interface))

问题集:

package com.jsoftware.test;

import java.io.Serializable;
import java.util.ArrayList;

public class QuestionSet implements Serializable, IQuestionSet{

    ArrayList<Question> test = new ArrayList<Question>();

    public QuestionSet emptyTestSet(){
        QuestionSet set = new QuestionSet();
        return set;
    }
    public QuestionSet randomSample(int size){
        QuestionSet set = new QuestionSet();
        if(size>test.size()-1){
            for(int i =1; i<test.size(); i++){
                int num = (int)(Math.random()*test.size());
                set.add(test.get(num));
            }
        }else{
            for(int i =1; i<size; i++){
                int num = (int)(Math.random()*test.size());
                set.add(test.get(num));
            }
        }
        return set;
    }
    public boolean add(Question q){
        try{
            test.add(q);
            return true;
        }catch(Exception e){
            return false;
        }
    }
    public boolean remove(int index){
        try{
            test.remove(index);
            return true;
        }catch(Exception e){
            return false;
        }
    }
    public Question getQuestion(int index){
        return test.get(index);
    }
    public int size(){
        return test.size();
    }


}

问题集:

package com.jsoftware.test;

/**
 * This interface represents a set of question. 
 * 
 * @author thaoc
 */
public interface IQuestionSet {

    /**
     * Create an empty test set.
     * @return  return an instance of a test set.
     */
    public IQuestionSet emptyTestSet();

    /**
     * return a test set consisting of a random questions.
     * @param size The number of random questions.
     * @return The test set instance containing the random questions.
     */
    public IQuestionSet randomSample(int size);

    /**
     * add a question to the test set.  
     * @param question The question
     * @return True if successful.
     */
    public boolean add(IQuestion question);

    /**
     * 
     * @param index Remove question using index
     * @return  true if index is valid
     */
    public boolean remove(int index);

    /**
     * Retrieving a question using an index
     * @param index
     * @return the question if index is valid, null otherwise.
     */
    public IQuestion getQuestion(int index);

    /**
     * Return the number of questions in this test set.
     * @return number of questions.
     */
    public int size();
}

最佳答案

首先将您的问题列表更改为使用 IQuestion 类型:

ArrayList<IQuestion> test = new ArrayList<IQuestion>();

然后更改方法声明:

public boolean add(IQuestion q){
    try{
        test.add(q);
        return true;
    }catch(Exception e){
        return false;
    }
}

关于java - 该类型必须实现继承的抽象方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36731363/

相关文章:

java - 如何检测请求的 URL 是否包含查询字符串?

java - 覆盖 toString 但仍然使用它来完成工作

go - 接口(interface)及其实现的包/目录结构

c# - 将对象转换为通用接口(interface)

java - ServerSocket.close()。它什么时候抛出异常?

java - spring中通过文件实现数据访问层

c++ - 当我们尝试使用和不使用虚函数访问向下转换的指针时发生了什么

typescript - 是否可以为 typescript 中的接口(interface)函数提供默认参数?

Java OutputStream.write() 抛出错误的文件描述符,但刷新有效

c++ - 隐藏私有(private)重载虚函数?