Java 类型必须实现继承的抽象方法
我正在为中间Java编写一个程序,我觉得我的逻辑是正确的,但是我在课程介绍中遇到了这个错误: 类型问题集必须实现继承的抽象方法 IQuestion.add(IQuestion) 我有这个方法,但是我有一个用于参数的Question对象,而不是IQuestion(Question的接口) 问题集:Java 类型必须实现继承的抽象方法,java,inheritance,interface,Java,Inheritance,Interface,我正在为中间Java编写一个程序,我觉得我的逻辑是正确的,但是我在课程介绍中遇到了这个错误: 类型问题集必须实现继承的抽象方法 IQuestion.add(IQuestion) 我有这个方法,但是我有一个用于参数的Question对象,而不是IQuestion(Question的接口) 问题集: package com.jsoftware.test; import java.io.Serializable; import java.util.ArrayList; public class Q
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>();
该方法在接口中的偏差不同,因此当您实现
公共布尔添加(问题)时代码>您没有覆盖公共布尔添加(IQuestion问题)代码>和抽象接口要求您实现它的所有方法(或者自己抽象)。请更改参数中的参数,或者将其设置为T implements IQuestion
public boolean add(IQuestion q){
try{
test.add(q);
return true;
}catch(Exception e){
return false;
}
}