简体   繁体   English

使用 super() 构造函数时,未初始化空白最终字段

[英]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中的最终变量p1p2没有在任何地方初始化!

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.

相关问题 java 子类的私有 final 字段可以在超级构造函数完成之前初始化吗? - Can a java subclass's private final field be initialized before the super constructor completes? 确保在构造函数抛出异常时初始化最终变量 - Making sure that a final variable is initialized when a constructor throws an exception 检测构造函数中的final是否为空 - Detecting if a final is blank in a constructor 如何使用 EasyMock 和 PowerMock 模拟构造函数内部初始化的最终变量? - How to mock final variable initialized inside constructor ,using EasyMock and PowerMock? 为什么final可以在构造函数中初始化? - Why final can be initialized in constructor? 这两个构造函数上的错误 空白的最终字段 errorCodes 可能尚未初始化 - error on this two constructor The blank final field errorCodes may not have been initialized 空白的final字段可能尚未初始化 - the blank final field may not have been initialized 使用 Eclipse 中的字段生成的构造函数中的 super 关键字的目的是什么? - What is the purpose of the super keyword in the Constructor generated using fields in Eclipse? 相对于超类构造函数的最终可修改初始化 - final vairable initialization relative to super class constructor hibernate如何使用no-arg构造函数初始化final字段? - How hibernate is initializing final fields using the no-arg constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM