更新时间:2023-01-17 12:48:31
只需给它提供无参数的no-args构造函数即可.这将是可变的,但比您提供所有二传手的方式要少一些混乱.当您使用 Kryo作为反序列化器(我认为您已经这样做)时,可以将此构造函数设为私有.
Just give it no-args constructor without any setters. It will be mutable but in slightly less chaotic fashion than if you provided all the setters. When you use Kryo as your deserializer (which I think you do already) you can keep this constructor private.
所有参数构造函数仍然可以使用空值和荒谬的值来调用.如果要对对象的有效性强加某些约定,请显式使用验证.如果您追求的是不变性,那么使用no-arg构造函数的成员将不再是最终的.
All-args constructor still can be called with nulls and insensical values. If you want to impose some contract on validity of the object, use validation explicitly. If it is immutability you are after, your members will not be final anymore using no-arg constructor.
相反,他们创建了vanila对象,并通过设置器或反射器将其填充,以确保名称在序列化版本和对象版本之间匹配.All-args构造函数执行此操作的可靠性较低,并且难以实现.
Instead they create the vanila object and populate it via setters or reflection making sure the names match between serialised and object versions. All-args constructor would do this less reliably and would be much harder to implement.
如果需要保持不变性,则必须使用自定义对象创建.请查看 Kryo用于创建自定义对象的示例:
If you need to keep your immutability, you have to use custom object creation. Please have a look at Kryo's example for custom object creation:
Registration registration = kryo.register(SomeClass.class);
registration.setInstantiator(new ObjectInstantiator<SomeClass>() {
public SomeClass newInstance () {
return new SomeClass("some constructor arguments", 1234);
}
});