[英]Blank final fields are not initialized when using a super()-constructor
I am admittedly a bit new to object-oriented programming and would therefore be very grateful for a little help.诚然,我对面向对象编程有点陌生,因此非常感谢您的帮助。 I have a class "Gerade" (German for straight line) and a class "Strahl" (German for ray).
我有一个 class“Gerade”(直线的德语)和一个 class“Strahl”(德语的射线)。 I would like to call the constructor of the superclass "Gerade" in the subclass "Strahl": But now I get the error.
我想在子类“Strahl”中调用超类“Gerade”的构造函数:但现在我得到了错误。 'The blank final field p1[and p2] may not have been initialized'?
'空白的最终字段 p1[和 p2] 可能尚未初始化'? What is the reason for this.
这是什么原因。 I am very grateful for any answers.
我非常感谢任何答案。 Thank you in advance!
先感谢您!
public class Gerade {
private final Punkt p1;
private final Punkt p2;
public Gerade(Punkt p1, Punkt p2)
{
if(p1.equals(p2)==true)
{
System.out.println("Die Eingabe ist ungueltig. Beide Punkte haben die selben Koordinaten.");
p1 = null;
p2 = null;
}
if(p1.getXkoord().equals(p2.getXkoord())==true)
{
if((p1.getYkoord().compareTo(p2.getYkoord()) < 0))
{
this.p1 = p1;
this.p2 = p2;
}
else
{
this.p1 = p2;
this.p2 = p1;
}
}
else if((p1.getXkoord().compareTo(p2.getXkoord()) > 0))
{
this.p2 = p1;
this.p1 = p2;
}
else
{
this.p1 = p1;
this.p2 = p2;
}
}
The class "Gerade" uses certain attributes and methods of the class "Punkt". class“Gerade”使用了 class“Punkt”的某些属性和方法。 The coordinates are BigDecimals.
坐标是 BigDecimals。 Just so youre not confused.
只是为了让您不感到困惑。
public class Strahl extends Gerade
{
private final Punkt p1;
private final Punkt p2;
private final Punkt start;
public Strahl(Punkt a, Punkt b)
{
super(a, b);
this.start = a;
}
Both p1 and p2 shall be initialized after constructor of Gerade is executed. p1 和 p2 都应在执行 Gerade 的构造函数后进行初始化。 But compiler can not assure that this will happen due to complicated control flow.
但是由于复杂的控制流程,编译器不能保证这会发生。 Look carefully at your conditions - you have to provide for the fallback if none of if conditions is satisfied
仔细查看您的条件 - 如果条件都不满足,您必须提供回退
Since you are calling super(a, b)
, there is no reason why you should redeclare them in Strahl
.由于您正在调用
super(a, b)
,因此没有理由在Strahl
中重新声明它们。 Instead of writing p1.equals(p2) == true
, you can simply write p1.equals(p2)
.您可以简单地编写
p1.equals(p2)
而不是编写p1.equals(p2) == true
。
It can be something as它可以是
class Gerade {
private final Punkt p1 = null;
private final Punkt p2 = null;
public Gerade(Punkt p1, Punkt p2) {
if (p1.equals(p2)) {
System.out.println("Die Eingabe ist ungueltig. Beide Punkte haben die selben Koordinaten.");
p1 = null;
p2 = null;
}
// ...
}
}
class Strahl extends Gerade {
private final Punkt start;
public Strahl(Punkt a, Punkt b) {
super(a, b);
this.start = a;
}
}
I also suggest that you use a constructor only for initialization and any business logic should be refactored into some methods.我还建议您仅将构造函数用于初始化,任何业务逻辑都应重构为一些方法。
You already have two Punkt
instance variables in your class Gerade
.您的 class
Gerade
中已经有两个Punkt
实例变量。 So you do not need to repeat them in your class Strahl
.因此,您无需在 class
Strahl
中重复它们。
In fact, this is one of the problems: The final variables p1
and p2
in Strahl
are not initialized anywhere!事实上,这是问题之一:
Strahl
中的最终变量p1
和p2
没有在任何地方初始化!
So, simply remove them:所以,只需删除它们:
public class Strahl extends Gerade {
private final Punkt start;
public Strahl(Punkt a, Punkt b) {
super(a, b);
this.start = a;
}
}
Another problem is what @KonstantinPribluda mentioned in his answer.另一个问题是@KonstantinPribluda 在他的回答中提到的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.