[英]initialise superclass array in subclass (java)
I am new to JAVA / Android programming and have a small problem. 我是JAVA / Android编程的新手,并且有一个小问题。
I created in a Superclass a Array and wanted to initialize it in multiple subclasses. 我在超类中创建了一个数组,并想在多个子类中对其进行初始化。 But when I try to initialize it, it says it's not possible. 但是当我尝试初始化它时,它说不可能。
My code: 我的代码:
public abstract class Fragen {
String[] Deutsch;
String[] Slowakisch;
static int Anzahl;
Random random;
int randNumber;
byte Fächer;
public String displayQuestion()
{
//TODO Fach abfragen
randNumber = random.nextInt(Anzahl);
return Slowakisch[randNumber];
}
public boolean correctAnswer(String answer)
{
//TODO Fächer +/-
if(answer.equals(Deutsch[randNumber]))
return true;
else
return false;
}
}
(Superclass) (超类)
public class Lektion1 extends Fragen
{
private Lektion1()
{
super();
Anzahl = 60;
//Deutsch = new String[];
Deutsch = {"",""};
Slowakisch = {"",""};
}
}
(Subclass) (子类)
Kind regards Thomas 亲切的问候托马斯
Try this: 尝试这个:
public class Lektion1 extends Fragen
{
private Lektion1()
{
super();
Anzahl = 60;
//Deutsch = new String[];
Deutsch = new String[]{"", ""};
Slowakisch = new String[]{"", ""};
}
}
Just create a constructor in your super class and initialize that array there: 只需在您的超类中创建一个构造函数并在那里初始化该数组:
public abstract class Fragen {
...
public Fragen() {
Deutsch = new String[];
}
...
}
alternative 替代
or if your fields have appropriate access modifiers, then you can also access them using super
keyword 或者,如果您的字段具有适当的访问修饰符,则也可以使用super
关键字来访问它们
public abstract class Fragen {
public String[] Deutsch;
...
...
}
and in base class: 在基类中:
public class Lektion1 extends Fragen {
private Lektion1()
{
super();
Anzahl = 60;
super.Deutsch = new String[];
Deutsch = {"",""};
Slowakisch = {"",""};
}
}
Edit: For a conrete answer to your concrete question, @MrQuattro's answer is of course correct and fulfilling. 编辑:对于您的具体问题的确切答案,@ MrQuattro的答案当然是正确且令人满意的。
That said, if you want to learn more of object-oriented design, I still myself like the ideas from my original answer. 就是说,如果您想了解更多有关面向对象设计的知识,我仍然喜欢我最初回答中的想法。 You can of course throw away or use as you see fit: I propose that it will be more convenient to keep the German and the Slovakian word together, so I'd make a class Frage
to hold one word (or phrase or expression) in both languages. 当然,您可以随意扔掉或使用:我建议将德语和斯洛伐克语的单词放在一起会更方便,所以我将让Frage
类在其中包含一个单词(或短语或表达)。两种语言。
Next I'd want to make a constructor in Fragen
that accepts all the Frage
objects, for instance as varargs (you should learn about those). 接下来,我想在Fragen
中创建一个接受所有Frage
对象的构造函数,例如,作为varargs(您应该了解这些对象)。 This will allow the Lektion1
constructor to contain just 这将使Lektion1
构造函数只包含
super(new Frage("", ""), new Frage("", ""), new Frage("", ""));
Fragen
can count, so you don't need to: the superclass constructor will set anzahl
(please use lowercase a
for a variable) to the number of questions received. Fragen
可以计数,因此您不需要:超类构造函数将anzahl
(请为变量使用小写字母a
)设置为收到的问题数。
If you want to take it a step further, the Frage
class could also be given the responsibility of checking whether an answer is correct. 如果您想更进一步, Frage
类也可以负责检查答案是否正确。
Link: Anti-pattern: parallel collections 链接: 反模式:并行集合
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.