这是一个外部I类需要扩展:Java的子类的构造
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;
}
这是我所创建的子类:
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);
}
}
我想迫使我所有的子类(NoahId)至有一定长度的byte []数据,如果没有则抛出异常。如果构造函数调用必须是子类构造函数中的第一个语句,我该如何执行这种检查?
使用静态方法来创建我的类允许我做检查,但我仍然必须定义一个显式构造函数。
为什么你不检查`super`调用之后的长度?之后您仍然可以抛出错误。 – 2011-01-11 01:40:44