1

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

假设我想创建一个多项选择题,但是我不知道我的用户想输入多少个答案,也许是 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 覆盖有关,但无论如何,请告诉我这个问题,这样我就不必担心一个不那么令人困惑的问题了。

4

3 回答 3

5

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

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()通常是多余的(你不需要它们)。

于 2017-03-09T09:43:05.363 回答
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;

    }

它看起来有点井井有条。

于 2017-03-09T09:48:07.763 回答
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;
    }

那么你可以做两件事,

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

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

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

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

这将有助于提高可读性。

于 2017-03-09T10:22:05.230 回答