简体   繁体   English

为什么在通过元类类工厂实例化时不会调用派生的构造函数?

[英]Why doesn't the derived constructor get called when instantiating via a metaclass class factory?

I'm trying to create what I understand to be a Class Factory in Delphi 2007. I want to pass a derived class type into a function and have it construct an object of that class type. 我正在尝试创建我所理解的Delphi 2007中的类工厂。我想将派生类类型传递给函数并让它构造该类类型的对象。

I've found some good references, such as How can I create an Delphi object from a class reference and ensure constructor execution? 我找到了一些很好的参考资料,例如如何从类引用中创建Delphi对象并确保构造函数执行? , but I still can't get it to work quite right. ,但我还是不能让它工作得很好。 In the test below, I can't get it to call the derived constructor, even though the debugger tells me that oClass is TMyDerived. 在下面的测试中,我无法让它调用派生构造函数,即使调试器告诉我oClass是TMyDerived。

I think I'm confused about something fundamental here and could use some explanation. 我觉得我对这里的一些基本内容感到困惑,可以使用一些解释。 Thanks. 谢谢。

program ClassFactoryTest;
{$APPTYPE CONSOLE}
uses
  SysUtils;

//  BASE CLASS
type
  TMyBase = class(TObject)
    bBaseFlag : boolean;
    constructor Create; virtual;
  end;
  TMyBaseClass = class of TMyBase;

constructor TMyBase.Create;
begin
  bBaseFlag := false;
end;

//  DERIVED CLASS
type
  TMyDerived = class(TMyBase)
    bDerivedFlag : boolean;
    constructor Create;
  end;

constructor TMyDerived.Create;
begin
  inherited;
  bDerivedFlag := false;
end;

var
  oClass: TMyBaseClass;
  oBaseInstance, oDerivedInstance: TMyBase;
begin
  oClass := TMyBase;
  oBaseInstance := oClass.Create;

  oClass := TMyDerived;
  oDerivedInstance := oClass.Create;  // <-- Still calling Base Class constructor
end.

You neglected to specify override on the derived class constructor. 您忽略了在派生类构造函数上指定override (I would have expected a warning from the compiler about hiding the base-class method.) Add that, and you should see TMyDerived.Create called. (我本来期望编译器发出关于隐藏基类方法的警告。)添加它,你应该看到调用了TMyDerived.Create

TMyDerived = class(TMyBase)
  bDerivedFlag : boolean;
  constructor Create; override;
end;

An alternative, since your constructors take no parameters, is to forgo virtual constructors and just override AfterConstruction . 另一种选择,因为你的构造函数不带参数,就是放弃虚拟构造函数,然后重写AfterConstruction

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

相关问题 Delphi XE:使用泛型在类中不会调用类构造函数 - Delphi XE: class constructor doesn't get called in a class using generics 为什么在使用TEmbeddedWB时QueryService不被IHttpSecurity调用? - Why doesn't QueryService get called for IHttpSecurity when using TEmbeddedWB? 为什么不调用后代的抽象基类方法版本? - Why doesn't the descendent's version of an abstract base class's method get called? 在 class 构造函数中完成 class 注册时,未调用 Class 构造函数 - Class constructor not called when class registration is done in that class constructor 如何从Delphi 6对象中获取用于分配给元类变量的类? - How to get the class from a Delphi 6 Object for assigning to a metaclass variable? Delphi - 确保调用 class 构造函数 - Delphi - Ensure a class constructor is called 当一个类实现一个后代接口时,为什么它不会自动计为实现基接口? - When a class implements a descendant interface, why doesn't it automatically count as implementing the base interface? 当类方法使用self时,为什么Delphi编译器不发出警告? - Why doesn't the Delphi compiler throw a warning when a class method uses self? 从 TIdTCPServer 调用时 TIdNotify.Notify() 不起作用 - TIdNotify.Notify() doesn't work when called from TIdTCPServer 为什么当我调用 Trim 时我的字符串没有被修剪? - Why doesn't my string get trimmed when I call Trim on it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM