我应该在从TObject或TPersistent派生的类的构造函数中调用"inherited"吗?
constructor TMyObject.Create;
begin
inherited Create; // Delphi doc: Do not create instances of TPersistent. Use TPersistent as a base class when declaring objects that are not components, but that need to be saved to a stream or have their properties assigned to other objects.
VectorNames := TStringList.Create;
Clear;
end;
Run Code Online (Sandbox Code Playgroud)
Cra*_*ntz 39
是.它什么都不做,是的,但它是无害的.我认为总是调用继承的构造函数是一致的,而不检查实际上是否存在实现.有人会说,值得调用继承的Create,因为Embarcadero将来可能会为TObject.Create添加一个实现,但我怀疑这是真的; 它会破坏不调用继承Create的现有代码.尽管如此,我认为仅仅因为一致性而称它为一个好主意.
Too*_*the 13
我总是这样做.
如果要重构并将代码移动到共同的祖先,则调用继承的Create具有以下优点: