【问题标题】:the type must implement the inherited abstract method该类型必须实现继承的抽象方法
【发布时间】:2016-04-19 23:44:35
【问题描述】:

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

类型 QuestionSet 必须实现继承的抽象方法 IQuestionSet.add(IQuestion)

我确实有方法,但我有一个参数的 Question 对象而不是 IQuestion(The Interface for Question)

问题集:

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();
    }


}

IQuestionSet:

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();
}

【问题讨论】:

  • 方法在接口中的声明是不同的,因此当你实现public boolean add(Question question);时你没有覆盖public boolean add(IQuestion question);并且抽象接口需要你实现它的所有方法(或者自己抽象) .要么更改参数中的参数,要么将其设为T implements IQuestion

标签: java inheritance interface


【解决方案1】:

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

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

然后更改方法声明:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-12
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多