简体   繁体   English

如何自动将用户定义的对象存储到地图中?

[英]How do I automatically store user defined objects into a map?

I'm creating a little program that allows you to insert your own multiple choice questions and which can ask you these questions by another method.我正在创建一个小程序,允许您插入自己的多项选择问题,并可以通过另一种方法向您询问这些问题。 So I set up my "Question Class" with the constructor, a toString() method and a method that can ask me these questions.所以我用构造函数、一个 toString() 方法和一个可以问我这些问题的方法设置了我的“问题类”。 My problem now is that I have to store the questions in a certain way, because one of the questions parameters is the integer "priority" which changes if you answered it right or wrong.我现在的问题是我必须以某种方式存储问题,因为问题参数之一是整数“优先级”,如果您回答正确或错误,它就会改变。

I thought about a map as you can see below but I don't know how I have to set it up properly, so it stores a new created question automatically into this map.我想到了一张地图,如下所示,但我不知道如何正确设置它,因此它会自动将新创建的问题存储到这张地图中。 Maybe I have to create another method that does that, but I would like to find a way that excludes calling an extra method.也许我必须创建另一个方法来做到这一点,但我想找到一种排除调用额外方法的方法。 The code below shows how I create a new question in the main method and the data fields and the constructor of the Question class.下面的代码显示了我如何在 main 方法和数据字段以及 Question 类的构造函数中创建一个新问题。

So in this example I'd like to save the question number1 into the Map database.所以在这个例子中,我想将问题 number1 保存到 Map 数据库中。 I don't want to do that manually.我不想手动执行此操作。

public static void main(String[] args) {

Question number1 = new Question("What is the right answer?",
            new String[] { "1", "2", "3", "4" }, 3, 1.0);
}

public class Question {

public String question;
public String[] answers;
public int solution;
public double priority;
public static Map<Integer, Question> Database = new TreeMap<Integer,Question>();

public Question(String que, String[] ans, int sol, double prio){
this.question = que;
this.answers = ans;
this.solution = sol;
this.priority = prio;

}

From your comments, to automatically insert it into the map use :根据您的评论,要自动将其插入地图,请使用:

public class Question {

   static int keyCount; 

   public Question(String que, String[] ans, int sol, double prio){
      this.question = que;
      this.answers = ans;
      this.solution = sol;
      this.priority = prio;
      Database.put(++keyCount, this);
   }

}

Here, we insert an entry in the map everytime you create a new object.在这里,每次创建新对象时,我们都会在地图中插入一个条目。

  1. The this refers to the current object created. this指的是当前创建的对象。
  2. The static variable keyCount is used to increment the value of the key everytime an object is created.静态变量keyCount用于在每次创建对象时增加键的值。

I would suggest against adding the object to Map inside the constructor.我建议不要将对象添加到构造函数中的 Map 中。 One reason would be the Single Responsibility principle.原因之一是单一职责原则。 Your constructor would do 2 things (initialize an object and add it to the Map).您的构造函数会做两件事(初始化一个对象并将其添加到 Map)。 This is a bad practice especially because the name of the method (the constructor in your case) does not state clearly what it does.这是一个不好的做法,特别是因为方法的名称(在您的情况下是构造函数)没有清楚地说明它的作用。 Another reason is the use of "this" inside a constructor.另一个原因是在构造函数中使用了“this”。 You should be very careful doing so because it can lead to issues really difficult to debug.这样做时应该非常小心,因为它会导致非常难以调试的问题。 The reason is: while you are still in the constructor, your object (so "this") is not yet fully initialized.原因是:当您仍在构造函数中时,您的对象(即“this”)尚未完全初始化。 So you are passing an object that is not fully initialized as a parameter to a method.因此,您将一个未完全初始化的对象作为参数传递给方法。 As I said, that can lead to big problems.正如我所说,这可能会导致大问题。

If you really need to do all in one you can create a static method in your class like this:如果你真的需要做所有的事情,你可以在你的类中创建一个静态方法,如下所示:

   public static addQuestion(String que, String[] ans, int sol, double prio){
      int key = Database.size();
      Database.put(key, new Question (que, ans, sol, prio));
   }

You can then call it like so:然后你可以这样称呼它:

   Question.addQuestion("What is the right answer?",
            new String[] { "1", "2", "3", "4" }, 3, 1.0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM