简体   繁体   English

Java子类构造函数

[英]Java subclass constructor

This is an external class I need to extend: 这是我需要扩展的外部类:

public class Binary {

    public Binary( byte type , byte[] data ){
        _type = type;
        _data = data;
    }

    public byte getType(){
        return _type;
    }

    public byte[] getData(){
        return _data;
    }

    public int length(){
        return _data.length;
    }

    final byte _type;
    final byte[] _data;
}

And this is the subclass I have created: 这是我创建的子类:

import org.bson.types.Binary;

public class NoahId extends Binary {

 public NoahId(byte[] data) {
  //Constructor call must be the first statement in a constructor 
  super((byte) 0 , data);
 }
}

I want to force all my subclasses (NoahId) to have a byte[] data of a certain lenght or throw an Exception if not. 我想强制所有子类(NoahId)具有某个长度的byte []数据,否则抛出异常。 How can I perform this kind of checks if a constructor call must be the first statement in a subclass constructor? 如果构造函数调用必须是子类构造函数中的第一个语句,我该如何执行此类检查?

Using a static method to create my class allows me to do the check but I still have to define an explicit constructor. 使用静态方法创建我的类允许我进行检查,但我仍然需要定义一个显式构造函数。

You can do the check and throw the exception after the call to super() . 你可以在调用super()之后进行检查并抛出异常。 If an exception is thrown at any point during the constructor, the object will be discarded and unavailable to callers. 如果在构造函数期间的任何时刻抛出异常,则该对象将被丢弃并且对调用者不可用。

If you're concerned about efficiency, you can write a static method that does the check and throws the exception, something like this: 如果您关注效率,可以编写一个静态方法来执行检查并抛出异常,如下所示:

super((byte) 0 , doChecks(data));

doChecks would return data unchanged if it's okay, otherwise it would throw an exception. 如果没有问题, doChecks会保持data不变,否则会引发异常。

Make the constructor private so only your factory method can see it and do the check in the factory method. 使构造函数为private因此只有工厂方法可以看到它并在工厂方法中进行检查。 As an added bonus, the stack trace from the exception will be (slightly) nicer. 作为额外的奖励,来自异常的堆栈跟踪将(稍微)更好。

You could always throw the exception after calling the superclass constructor. 在调用超类构造函数之后,您总是可以抛出异常。 This will abort construction and the client won't be able to see the malformed object. 这将中止构造,客户端将无法看到格式错误的对象。 Or is there some reason that you can't call the base class constructor without being sure the data has the right length? 或者是否有一些原因导致您无法在不确定数据长度合适的情况下调用基类构造函数?

Alternatively, if the restrictions are always the same, you could create a private constructor for your base class that does the integrity checks. 或者,如果限制始终相同,则可以为基类创建一个执行完整性检查的私有构造函数。

您的设计是否允许构造函数为private并通过静态create()方法强制构造?

Because Super class constructor call must be the first statement in a constructor, there is no way to insert a statement before super(); 因为超类构造函数调用必须是构造函数中的第一个语句,所以无法在super();之前插入语句super();

You can always do a check after the super() call, abort the constructor call by throwing an IllegalArgument exception if the length does not meet the requirement, the instance of the subclass will be created and completed only if the constructor call is finished. 你总是可以在super()调用之后进行检查,如果长度不符合要求,则通过抛出IllegalArgument异常来中止构造函数调用,只有在构造函数调用完成时才会创建并完成子类的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM