以这种方式使用多个构造函数是否正确?

Is it correct to use multiple constructors this way?

我不太确定这是如何工作的,但是如果我想提供为 class 的对象提供更多或更少变量的选项,这是否适用于像这样的多个构造函数?

假设我想创建一个多项选择问卷,但我不知道我的用户想要输入多少个答案,也许是 2、3、4、5、6?因此:

public class Quiz {
    private int counter;
    private String question;
    private String answer1;
    private String answer2;
    private String answer3;
    private String answer4;
    private String answer5;
    private String answer6;
    private String rightAnswer;

    public Quiz(int counter,String question, String answer1, String answer2, String rightAnswer){
        super();
        this.counter = counter;
        this.question = question;
        this.answer1 = answer1;
        this.answer2 = answer2;
        this.rightAnswer = rightAnswer;
    }
    public Quiz(int counter, String question, String answer1, String answer2, String answer3, String rightAnswer) {
        super();
        this.counter = counter;
        this.question = question;
        this.answer1 = answer1;
        this.answer2 = answer2;
        this.answer3 = answer3;
        this.rightAnswer = rightAnswer;
    }
    public Quiz(int counter, String question, String answer1, String answer2, String answer3, String answer4,
                String rightAnswer) {
        super();
        this.counter = counter;
        this.question = question;
        this.answer1 = answer1;
        this.answer2 = answer2;
        this.answer3 = answer3;
        this.answer4 = answer4;
        this.rightAnswer = rightAnswer;
    }
    //...more options

也许我可以用某种枚举或开关做 1 个构造函数? 归根结底,在尝试了这种方法之后,出于某种原因将其放入哈希图中,然后将其序列化到文件中 不起作用 与 1 个构造函数一样,它可以工作但没有'不要把所有东西都写在那里。我对问题是什么感到有点困惑,也许这与我的 toString 覆盖有关,但无论如何,请告诉我这个问题,这样我就不用担心另一个令人困惑的问题了。

对于您发布的代码,这将是一个简单的方法:

package com.steve.research;

public class Quiz {

    private int counter;
    private String question;
    private String answer1;
    private String answer2;
    private String answer3;
    private String answer4;
    private String answer5;
    private String answer6;
    private String rightAnswer;

    public Quiz(int counter, String question, String answer1, String answer2, String rightAnswer) {
        this(counter, question, answer1, answer2, null, null, rightAnswer);
    }

    public Quiz(int counter, String question, String answer1, String answer2, String answer3, String rightAnswer) {
        this(counter, question, answer1, answer2, answer3, null, rightAnswer);
    }

    public Quiz(int counter, String question, String answer1, String answer2, String answer3, String answer4, String rightAnswer) {
        this.counter = counter;
        this.question = question;
        this.answer1 = answer1;
        this.answer2 = answer2;
        this.answer3 = answer3;
        this.answer4 = answer4;
        this.rightAnswer = rightAnswer;
    }
}

为了改进方法,我建议您查看 "varargs" 中的问题。由于您有可变数量的问题,您可以将 String ... questions 作为最后一个构造函数参数(因此 rightAnswer 必须在前面)。

public class Quiz {

    private int counter;
    private String question;
    private String rightAnswer;
    private String[] answers;

    public Quiz(int counter, String question, String rightAnswer, String... answers) {
        this.counter = counter;
        this.question = question;
        this.rightAnswer = rightAnswer;
        this.answers = answers;
    }

    public static void main(String[] args) {
        new Quiz(1, "one plus one", "two", "one", "two", "three");
        new Quiz(1, "one plus one", "two", "one", "two", "three", "four");
        new Quiz(1, "one plus one", "two", "one", "two", "three", "four", "five");
    }
}

请注意,answers现在是一个字符串数组String[],您可以引用answers.lengthanswers[0]等。

还有一条评论:在构造函数中调用无参数 super() 通常是多余的(您不需要它们)。

为什么不使用答案列表。

 public int Quiz(int counter, List<String> answers, String rightAnswer){...}

你也可以使用像

这样的覆盖构造函数
public Quiz(int counter,String question, String answer1, String answer2, String rightAnswer){
    super();
    this.counter = counter;
    this.question = question;
    this.answer1 = answer1;
    this.answer2 = answer2;
    this.rightAnswer = rightAnswer;
    }

public Quiz(int counter,String question, String answer1, String answer2, String answer3,String rightAnswer){
    this(counter,answer1,answer2,rightAnswer);
    this.answer3 = answer3;

    }

它会看起来没有条理。

创建一个构造函数来捕获所有值,例如,

public Quiz(int counter,String question, String answer1, String answer2, String answer3,String rightAnswer){
    super();
    this.counter = counter;
    this.question = question;
    this.answer1 = answer1;
    this.answer2 = answer2;
    this.answer3 = answer3;
    this.rightAnswer = rightAnswer;
    }

然后你可以做两件事,

1:创建其他构造函数,并在其中使用上面创建的构造函数。

public Quiz(int counter,String question, String answer1, String answer2,String rightAnswer){
 this(counter,question, answer1, answer2, null, rightAnswer)
}

2:为每个点赞创建单独的静态方法

public Quiz getQuizeWithTwoAnswers(int counter,String question, String answer1, String answer2,String rightAnswer){
    return new Quiz(counter,question, answer1, answer2, null, rightAnswer)}

这将有助于提高可读性。