1
자식에서 부모 클래스로 값을 저장하는 방법을 알고 싶습니다. 여기의 미세 개까지자식에서 부모 클래스까지 값 저장
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'이 비어있을 수도 있습니다. 막연한 예에서 무슨 일이 벌어지고 있는지 말하기는 정말 어렵습니다. – Eugene
방금 더 자세한 내용으로 편집했습니다. 감사! – Cowarrior