带有子类私有变量的java继承

ql3eal8s  于 2021-08-20  发布在  Java
关注(0)|答案(2)|浏览(205)

UML图
因为父类没有“answer”变量,而子类有私有的“answer”变量,所以我一直在为我的java项目苦苦挣扎。即使父类中有一个getanswer方法,uml图的父类中也没有答案变量。如有任何建议,我们将不胜感激
下面是连接父类及其子类的java文件。子类中只有构造函数,而父类只有一个构造函数和两个getter。

import java.util.ArrayList;
import java.util.Random;
/**
 *
 * @author Admin
 */

public class SimpleQuiz {
    private ArrayList<Question> myQuestions;
    private int currentQuestion;

    public SimpleQuiz()
    {
        myQuestions =new ArrayList<Question>();

        myQuestions.add(new ShortAnswerQuestion("1. What is the color of apple?","Red"));
        myQuestions.add(new ShortAnswerQuestion("2. What is my favorite food?","Bacon"));
        myQuestions.add(new FillinBlankQuestion("3. I only have to _____ my head above water one more week","keep"));
        myQuestions.add(new FillinBlankQuestion("4. eight times _____ is equal to fourty","five"));
         myQuestions.add(new TrueFalseQuestion("5. Every animal has a tail","False"));
         myQuestions.add(new TrueFalseQuestion("6. A baby has more bones than an average adult","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);
        }

        //Returns true if this quiz has another question
        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++;
        }

        //Shuffle the list
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());  //Choose a random object in the list
                   randomList.add(inputList.get(randomIndex));   //add it to the new, random list
                   inputList.remove(randomIndex);   //remove to avoid duplicates
                }

                return randomList;  //return the new random list
        }
}
aij0ehis

aij0ehis1#

我不确定你从哪里得到了uml图,但是它可以被优化。看看问题陈述,问题不应该是一个类,如果不是抽象类,它应该是一个接口,并且不应该有任何示例变量和构造函数。在超类中有一个示例变量'question'是没有意义的。问题超类应该作为一个标记,下面是代码

interface Question{

String getQuestion();

String getAnswer();

boolean checkAnswer(String str);
}

现在,实现类需要提供方法的定义。

vc9ivgsu

vc9ivgsu2#

我不清楚这个问题,你们有uml,需要在代码中实现精确的定义吗?
如果是,我认为只有3种选择:
缺失字段 answer 父母
父对象是抽象的,并且 getAnswer() 是一种抽象的方法
在父实现方法中 getAnswer() 为空,并在children中重写它并返回children的私有答案

相关问题