我在这里使用接口时遇到问题。当我尝试设置列表"答案"到ArrayList我得到错误:
QuestionImpl类型中的方法setAnswers(List)不适用于参数(ArrayList)
对我而言,似乎我想在这种情况下使用接口,以便稍后指定实现。
public class QuestionImpl extends QuestionAnswerImpl implements Comparable<Object>, Question {
private List<Answer> answers;
public QuestionImpl(){
setAnswers(new ArrayList<AnswerImpl>());
}
@Override
public void setAnswers(List<Answer> answers) {
this.answers = answers;
}
@Override
public List<Answer> getAnswers() {
// TODO Auto-generated method stub
return answers;
}
回答界面:
public interface Answer {
int i=0;
public int compareTo(Object object1);
public String getListValu(int question);
public void setNum(int i);
}
Answer class info (most of the code is irrelevant here)
public class AnswerImpl extends QuestionAnswerImpl implements
Comparable<Object>, Answer {...}
答案 0 :(得分:2)
您的类型参数错误。您的方法需要Answer
类型的列表,但是您传递的AnswerImpl
列表不同。这是两种不同的类型。您可以通过使用泛型类型参数来解决此问题,例如
@Override
public void setAnswers(List<? extends Answer> answers) {
this.answers = answers;
}