解决方案
是的。它什么也没有,但它是无害的。我认为总是调用继承的构造函数是一致的,而不检查实际上是否存在实现。有些人会说,值得调用继承的Create,因为Embarcadero将来可能会为TObject.Create添加一个实现,但我怀疑这是真的;它会破坏不调用继承的Create的现有代码。不过,我认为这是一个好主意,因为一致的原因。
Should I call "inherited" in the constructor of a class derived from TObject or TPersistent?
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;解决方案
Yes. It does nothing, true, but it's harmless. I think there is value in being consistent about always calling the inherited constructor, without checking to see if there is, in fact, an implementation. Some will say that it's worth calling inherited Create because Embarcadero might add an implementation for TObject.Create in the future, but I doubt this is true; it would break existing code which does not call inherited Create. Still, I think it is a good idea to call it for the reason of consistency alone.
更多推荐
使用继承于“创建”一个TObject的构造函数
发布评论