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

时间:2017-03-09 09:34:13

标签: java multiple-constructors

我不确定这是如何工作的,但是如果我想给出一个类的对象提供更多或更少变量的选项,那么这可以使用像这样的多个构造函数吗?

让我们说我想创建一个多项选择问卷,但我不知道我的用户想要输入多少答案,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

也许我可以用一些enum或switch来做一个构造函数? 在一天结束时,在尝试此方法之后,出于某种原因将其放入散列映射然后将其序列化为文件不起作用,与1构造函数一样,它可以工作但不会把一切都写在那里。我对问题是什么感到困惑,也许它与我的toString覆盖有关,但无论如何,只要告诉我这个问题,这样我就可以担心一个不那么令人困惑的问题了。

3 个答案:

答案 0 :(得分:3)

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

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]等等。

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

答案 1 :(得分:0)

为什么不使用答案清单。

 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;

    }

它看起来很有条理。

答案 2 :(得分:0)

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

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;
    }

然后你可以做2件事,

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)}

这有助于提高可读性。