多态关系

时间:2019-06-29 10:01:14

标签: java polymorphism

我在继承和多态性方面是新手,所以我总是感到困惑。我的任务是编写一个简单的测验程序,该程序将包含3种类型的问题,即ShortAnswerQuestion,FillInBlankQuestion和TrueFalseQuestion。

我唯一的问题是如何从这三个班级获得答案。我可以显示问题,但无法弄清楚答案。

这是我的密码。

Question.class

public class Question{

private String question;


public Question(String question) {
    this.question = question;

}


public String getQuestion() {
    return question;
}


public String getAnswer() {
    return "";      // here I think is the problem. What should I put here to display the answer?
}


public boolean checkAnswer(String ans)
{
    return true; // Same as this one.
}

}

ShortAnswerQuestion.class

public class ShortAnswerQuestion extends Question {

private String answer;

public ShortAnswerQuestion(String question, String answer) {
    super(question);
    this.answer = answer;
   }

}

FillInBlankQuestion.class

public class FillInBlankQuestion extends Question{

private String answer;

public FillInBlankQuestion(String question, String answer) {
    super(question);
    this.answer = answer;
   }

}

TrueFalseQuestion.class

public class TrueFalseQuestion extends Question{


private String answer;

public TrueFalseQuestion(String question, String answer) {
    super(question);
    this.answer = answer;
}

}

SimpleQuiz.class

public class SimpleQuiz {

private ArrayList<Question> myQuestions;
private int currentQuestion;

public SimpleQuiz()
{
    myQuestions = new ArrayList<>();
    //some sample questions...
    myQuestions.add(new ShortAnswerQuestion("What is the color of apple?","RED"));
    myQuestions.add(new ShortAnswerQuestion("Who invents telephone?","Alexander Bell"));
    myQuestions.add(new FillInBlankQuestion("ML is a __________?\nFill in the Blanks","GAME"));
    myQuestions.add(new FillInBlankQuestion("COC is a __________?\nFill in the Blanks","GAME"));
    myQuestions.add(new TrueFalseQuestion("Oreo and Cream-O are the same.\nTrue or False","FALSE"));
    myQuestions.add(new TrueFalseQuestion("Is Mars habitable?\nTrue or False","TRUE"));


    myQuestions = shuffleList (myQuestions);
    currentQuestion = 0;
}


    public String getCurrentQuestion()
    {
        return myQuestions.get(currentQuestion).getQuestion();
    }


    public String getCurrentAnswer()
    {
        return myQuestions.get(currentQuestion).getAnswer();
    }


    public boolean checkCurrentAnswer(String answer)
    {
       return myQuestions.get(currentQuestion).checkAnswer(answer);
    }


    public boolean hasNext()
    {
        return currentQuestion < myQuestions.size() - 1;
    }

    public void next() throws Exception 
    {
        if(currentQuestion == myQuestions.size()-1)
        {
            throw new Exception("There are no more questions.");
        }
        currentQuestion++;
    }


private ArrayList<Question> shuffleList (ArrayList<Question> inputList)
    {
            ArrayList<Question> randomList = new ArrayList<Question>();

            Random r=new Random();
            int randomIndex=0;
            while(inputList.size() > 0)
            {
               randomIndex=r.nextInt(inputList.size());  
               randomList.add(inputList.get(randomIndex));   
               inputList.remove(randomIndex);   
            }

            return randomList;
    }

}

2 个答案:

答案 0 :(得分:0)

如果要能够“获得”答案,则“ answer”必须是对象的属性,并且需要一种返回该属性的方法。问题是您为问题的每个子类都赋予了“答案”属性。 由于任何类型的问题都有答案,因此您应该给此属性以及将其获得超类问题的方法(这就是您应该如何考虑多态性)。

然后您可以考虑在Question中添加一个方法tryAnswer,该方法将字符串作为参数,并检查其是否与问题的答案匹配。

答案 1 :(得分:0)

这是您要求的简化版本。
(多态性及其使用方法)

import java.util.ArrayList;
import java.util.List;

public class A04 {

public static void main(String[] args)
{
     List<Question> l = new ArrayList<>();
     l.add(new A04().new ShortAnswerQuestion("Who was the first man on earth?", "Adam"));
     l.add(new A04().new TrueFalseQuestion("Is java hard to learn ?", Boolean.FALSE));
     l.add(new A04().new Question("Question with no answer ?"));
     for(Question q: l)
     {   
         System.out.println(q.question);
         System.out.println(q.getAnswer());
     }        


}

class Question
{
    String question;
    Question(String question)
    {
        this.question = question;
    }

    public String getAnswer()
    {
        if(this instanceof ShortAnswerQuestion)
        { 
            //downcast since the value for answer is within SAQ
            return ((ShortAnswerQuestion) this).answer;
        };
        if(this instanceof TrueFalseQuestion)
        {
            boolean b = ((TrueFalseQuestion) this).answer;
            if(b) return "True!";
            return "False!";
        };

        return "There is no answer defined yet for the above question";
    }

}

class ShortAnswerQuestion extends Question
{

    String answer;
    ShortAnswerQuestion(String question, String answer)
    {
        super(question);
        this.answer = answer;
    }   
}

class TrueFalseQuestion extends Question
{

    Boolean answer;
    TrueFalseQuestion(String question, Boolean answer)
    {
        super(question);
        this.answer = answer;
    }   
}
}

输出

Who was the first man on earth?
Adam

Is java hard to learn ?
False!

Question with no answer ?
There is no answer defined yet for the above question
相关问题