无法在java中解析方法

时间:2014-04-01 20:09:40

标签: java

我有一个问题对象,里面有4个答案对象。

在question.java中我有一个方法:

public Answer getA() {
    return a;
}

我用另一种方法:

if (questions.get(randomNum).getA().isCorrect())
                System.out.println("Correct!");

其中问题是包含我的问题对象的arraylist。

这给了我一个“无法解决方法getA()”错误,我不确定原因。

供参考,

System.out.println(questions.get(randomNum));

完美地打印出问题和答案。

Question.java

public class Question {
    private String questionText;
    private Answer a, b, c, d;

    public Question(String questionText, Answer a, Answer b, Answer c, Answer d) {
        this.questionText = questionText;
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
    }

    public String getQuestionText() {
        return questionText;
    }

    public void setQuestionText(String questionText) {
        this.questionText = questionText;
    }

    public Answer getA() {
        return a;
    }

    public void setA(Answer a) {
        this.a = a;
    }

    public Answer getB() {
        return b;
    }

    public void setB(Answer b) {
        this.b = b;
    }

    public Answer getC() {
        return c;
    }

    public void setC(Answer c) {
        this.c = c;
    }

    public Answer getD() {
        return d;
    }

    public void setD(Answer d) {
        this.d = d;
    }

    public String toString() {
        return  questionText +
                "\nA) " + a +
                "\nB) " + b +
                "\nC) " + c +
                "\nD) " + d;
    }
}

Answer.Java

public class Answer {
    private String answerText;
    private boolean correct;

    public Answer(String answerText) {
        this.answerText = answerText;
        this.correct = false;
    }

    public String getAnswerText() {
        return answerText;
    }

    public void setAnswerText(String answerText) {
        this.answerText = answerText;
    }

    public boolean isCorrect() {
        return correct;
    }

    public void setCorrect() {
        this.correct = true;
    }

    public String toString() {
        return answerText;
    }
}

1 个答案:

答案 0 :(得分:3)

确保您的容器(使用泛型)包含问题类型:

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

这样JAVA就知道要调用哪种方法。