将继承的帧作为过程的参数传递

问题描述 投票:2回答:1

我有一个TPageControl,在我的主要形式中有N个TTabSheets,我用来嵌入几个TFrame后代。对于这些框架,我创建了一个“ TBaseFrame”,从中可以导出要在TabSheets中显示的各个框架,或多或少看起来像这样...

TBaseFrame = class(TFrame)

  • TBaseFrameDescendant1 = class(TBaseFrame)
  • TBaseFrameDescendant2 = class(TBaseFrame)
  • TBaseFrameDescendantN = class(TBaseFrame)

我正在为此苦恼的是:我想创建一个过程,该过程将任何TBaseFrameDescendants用作参数,创建给定的框架并将其显示在新的选项卡中。我从这样的东西开始...

procedure CreateNewTabSheetAndFrame( What do I put here to accept any of my TBaseFrameDescendants? )
var
  TabSheet: TTabSheet;

begin
  TabSheet := TTabSheet.Create(MainPageControl);
  TabSheet.Caption := 'abc';
  TabSheet.PageControl := MainPageControl;

// Here I want to create the given TBaseFrameDescendant, set the Parent to the above TabSheet and so on   
end;

猜我这里的主要问题是如何设置我的过程,以便我可以传入从我的TBaseFrame派生的任何帧,以便可以在过程中使用它,还是我朝错误的方向前进?

function oop delphi inheritance procedure
1个回答
3
投票

您需要使用所谓的元类。

type
  TBaseFrameClass = class of TBaseFrame;

procedure TMainForm.CreateNewTabSheetAndFrame(FrameClass: TBaseFrameClass)
var
  TabSheet: TTabSheet;
  Frame: TBaseFrame;
begin
  TabSheet := TTabSheet.Create(Self);
  TabSheet.PageControl := MainPageControl;
  Frame := FrameClass.Create(Self);
  Frame.Parent := TabSheet;
end;

请确保如果在任何框架类中声明了任何构造函数,则它们应源自TComponent中引入的虚拟构造函数。为了通过元类实例化以调用适当的派生构造函数,这是必需的。

© www.soinside.com 2019 - 2024. All rights reserved.