gpt4 book ai didi

java - 使用包含数据的新 Array 或 ArrayList 调用父类(super class)的构造函数?

转载 作者:搜寻专家 更新时间:2023-11-01 01:06:35 25 4
gpt4 key购买 nike

我正在尝试使用 ArrayList(preferred) 或已经包含信息的数组来调用父类(super class)的构造函数。我不知道我的语法错误是什么,或者你是否能做到?

我特别想在任一对象中插入“true”和“false”。就这两个。

public class TrueFalseQuestion extends MultipleChoiceQuestion
{
Answer myAnswer;
StringPrompt myPrompt;

//I can create, but not initialize data up here, correct?
//Tried creating an ArrayList, but cannot insert the strings before calling constructor

public TrueFalseQuestion()
{
/* Want to call parent constructor with "true" and "false" in array or ArrayList already*/
super(new String["true", "false"]);
...
}

我有一种感觉,这是一个 facepalm-er,但我就是想不通。我尝试了各种方法,但痛苦的是必须首先调用 super 构造函数,因此我没有机会初始化数据。

最佳答案

使用格式:

super(new String[] {"true", "false"});

如果 MultipleChoiceQuestion 包含这样的构造函数:

MultipleChoiceQuestion(String[] questionArray)

如果它包含一个带有 List 参数的重载构造函数,例如:

MultipleChoiceQuestion(List<String> questionList)

然后你可以使用:

super(Arrays.asList("true", "false"));

或者如果需要使用ArrayList:

super(new ArrayList<String>(Arrays.asList(new String[] { "true", "false" })));

关于java - 使用包含数据的新 Array 或 ArrayList 调用父类(super class)的构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13335482/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com