【问题标题】:Storing values from child to parent class将值从子类存储到父类
【发布时间】:2017-11-15 06:25:23
【问题描述】:

我想知道如何将值从子类存储到父类。

public class Question {

private HashMap<String,ArrayList<String>> mcqHashMap = new HashMap<String,ArrayList<String>>(); //storing question and options of MCQ
public ArrayList<String> rankingquestion = new ArrayList<String>();
public Scanner scan = new Scanner(System.in);
public ArrayList<String> correctMCQAnswer = new ArrayList<String>(); // will keeps the correct answer of each question
HashMap<String,ArrayList<String>> newmcqHashMap;

public HashMap<String, ArrayList<String>> getMCQQuestionAnswer() {
    System.out.println("NEW:" + mcqHashMap);
    return this.newmcqHashMap;

}

public void setMCQQuestionAnswer(HashMap<String, ArrayList<String>> mcqQuestionAnswer) {
    this.mcqHashMap = mcqQuestionAnswer;
    System.out.println("After setting" + this.mcqHashMap);
    newmcqHashMap = this.mcqHashMap;
}


}


public class MultipleChoiceQuestion extends Question {

String storeMultipleChoiceQuestion;
ArrayList<String> answersMCQ = new ArrayList<String>();
HashMap<String, ArrayList<String>> mcqQuestionAnswer = new HashMap<String, ArrayList<String>>();


//returns MCQQuestion
public String storeMultipleChoiceQuestion() {
    System.out.println("Enter the Multiple question: ");
    String MultipleChoiceQuestion = scan.nextLine();
    while(MultipleChoiceQuestion.isEmpty()) {
        System.out.println("Null in invalid");
        MultipleChoiceQuestion = scan.nextLine();   

    }
    return MultipleChoiceQuestion;
}

//creates MCQ Questions with mcq options
public void create() {
    String value = storeMultipleChoiceQuestion();
    try {
        System.out.println("Enter the number of choices for your multiple choice question:");
        int numberOfChoices = scan.nextInt();
        scan.nextLine();
        if(numberOfChoices <= 0) {
            System.out.println("Must be greater than 0");
        }
        else {
            for(int i=1; i <= numberOfChoices; i++){
                System.out.println("Enter prompt " + i);
                String Option = scan.nextLine();
                answersMCQ.add(Option);
                while(Option.isEmpty()) {
                    System.out.println("Null in invalid");
                     Option = scan.nextLine();
                }
                mcqQuestionAnswer.put(value, answersMCQ);
                super.setMCQQuestionAnswer(mcqQuestionAnswer);
                //this.mcqHashMap.put(value, answersMCQ);                   
            }
        }
        }catch (Exception e) {
        System.out.println("Exception thrown! Use integer greater than 0.");
        }
        }

到这里为止还好...

现在,当我尝试从另一个子类调查访问时。

public class Survey extends Question implements Serializable {


MultipleChoiceQuestion mcq = new MultipleChoiceQuestion();
public void Display() {

    System.out.println("THis is from get " + this.getMCQQuestionAnswer());
    System.out.println("THis is from get mcqhash" + this.newmcqHashMap);

}

` 不打印任何东西...

我确定我在某处做错了。我还在父类中使用了getter setter,但仍然没有运气。所以基本上,问题将存储到 mcqHashMap 中,我想使用存储的 mcqHashMap 来显示 Survey 类的目的。

感谢您的帮助。谢谢

【问题讨论】:

  • 如果你说那是打印任何东西,可能mcqHashMap 是空的吗?从你模糊的例子中很难看出发生了什么
  • 我刚刚编辑了更多细节。谢谢!

标签: inheritance hashmap abstract-class


【解决方案1】:

没关系,伙计们,我想通了。谢谢!我制作了受保护的静态而不是受保护的哈希图。不好的做法,但有效!

感谢您的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多