我已经检查了该站点,似乎其他人也遇到了类似的问题,但是他们的问题是由于静态字段引起的.我有2个班级,一个是Questions类,另一个是Questions类的ScienceQuestions类:
I've checked through this site and it seems other people have had similar problems but theirs were due to static fields. I have 2 classes, a Questions class and a ScienceQuestions class which extends Questions:
import java.util.*; public abstract class Questions { public int questionID; public int difficulty; public boolean bAsked; public String question; public ArrayList<String> answers; public String correctAnswer; public Questions addQuestion(int id, int dif, boolean asked, String q, ArrayList<String> ans, String correct) { questionID = id; difficulty = dif; bAsked = asked; question = q; answers = ans; correctAnswer = correct; return this; }}
import java.util.ArrayList; public class ScienceQuestions extends Questions { public ArrayList<Questions> sciQuestions = new ArrayList<Questions>(); //Add default questions public ScienceQuestions() { sciQuestions.add(0, addQuestion1()); sciQuestions.add(1, addQuestion2()); sciQuestions.add(2, addQuestion3()); System.out.println(sciQuestions.get(0).question + " " + sciQuestions.get(1).question + " " + sciQuestions.get(2).question); } //question 1 private Questions addQuestion1() { int questionID = 1; int dif = 1; boolean asked = false; String question = "What is the chemical symbol for Magneisum?"; ArrayList<String> answers = new ArrayList<>(); String answer1 = "Mg", answer2 = "M", answer3 = "mg", answer4 = "MG"; String correctAnswer = answer1; answers.add(answer1); answers.add(answer2); answers.add(answer3); answers.add(answer4); Questions thisQ = addQuestion(questionID, dif, asked, question, answers, correctAnswer); return thisQ; } //question 2 private Questions addQuestion2() { int questionID = 1; int dif = 2; boolean asked = false; String question = "What is the most accurate acceleration on Earth due to gravity as sea level?"; ArrayList<String> answers = new ArrayList<>(); String answer1 = "9.81 m/s", answer2 = "9.81 N/Kg", answer3 = "10 m/s^2", answer4 = "10 N/Kg"; String correctAnswer = answer2; answers.add(answer1); answers.add(answer2); answers.add(answer3); answers.add(answer4); Questions thisQ = addQuestion(questionID, dif, asked, question, answers, correctAnswer); return thisQ; } //question 2 private Questions addQuestion3() { int questionID = 1; int dif = 2; boolean asked = false; String question = "What is the order of magnitude of the gravitational constant in standard units?"; ArrayList<String> answers = new ArrayList<>(); String answer1 = "*10^-10", answer2 = "*10^-13", answer3 = "*10^-12", answer4 = "*10^-11"; String correctAnswer = answer4; answers.add(answer1); answers.add(answer2); answers.add(answer3); answers.add(answer4); Questions thisQ = addQuestion(questionID, dif, asked, question, answers, correctAnswer); return thisQ; } }现在,当我实例化ScienceQuestions类时,ArrayList sciQuestions中的所有元素都与我添加的最后一个元素相同.为什么会这样?
Now when I instantiate the ScienceQuestions class all of the elements in the ArrayList sciQuestions are of the same as the final element the I've added. Why is that?
推荐答案您仅创建一个实例并覆盖属性.
You create only one instance and overwrite the properties.
您可以:
import java.util.*; public class Questions { public int questionID; public int difficulty; public boolean bAsked; public String question; public ArrayList<String> answers; public String correctAnswer; public Questions(int id, int dif, boolean asked, String q, ArrayList<String> ans, String correct) { questionID = id; difficulty = dif; bAsked = asked; question = q; answers = ans; correctAnswer = correct; } }并在ScienceQuestions.class中删除extends Questions并替换
and in ScienceQuestions.class remove the extends Questions and replace
addQuestion(questionID, dif, asked, question, answers, correctAnswer);与
new Questions(questionID, dif, asked, question, answers, correctAnswer);更多推荐
Java ArrayList.add()用最终元素替换数组中的所有元素
发布评论