設計上の問題があります:
- 質問クラス: 質問と回答があります (文字列)
- TextQuestion クラス: 通常のテキストの質問と回答
- NumberQuestion クラス: 質問 (文字列)、回答 (数値 - int、float...)、イプシロン値を使用した回答の確認
しかし、NumberQuestion には問題があります。正しい答えを数値に変換し、ユーザーからの答えと比較する必要があります。では、どうすれば答えを得ることができますか?getter メソッドの使用は安全ではありません。それとも私のデザインが良くないですか?どうすれば変更できますか?
public abstract class Question { private String question; private String answer; public Question(String question, String answer) { this.question = question; this.answer = answer; } public boolean checkAnswer(String yourAnswer) { // default implementation } } class TextQuestion extends Question { // Answer and Question is always string type, it's OK, not problem } class NumberQuestion extends Question { // Question is String, OK // Answer: input is number, accept approximately // Ex: Question: 1/3 = ? // Answer: 0.3 or 0.33 or 0.333 are accepted (using an Epsilon value) // so must override checkAnswer public boolean checkAnswer(String yourAnswer) { // HOW can I do? } }