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

标签 function oop delphi inheritance procedure

我的主窗体中有一个 TPageControl,其中包含 N 个 TTabSheets,我用它来嵌入多个 TFrame 后代。 对于我创建的“TBaseFrame”框架,我从中派生出要在 TabSheets 中显示的各个框架,或多或少看起来像这样......

TBaseFrame = 类(TFrame)

  • TBaseFrameDescendant1 = 类(TBaseFrame)
  • TBaseFrameDescendant2 = 类(TBaseFrame)
  • TBaseFrameDescendantN = 类(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 派生的任何框架,以便我可以在程序中使用它,或者我在这里走向错误的方向?

最佳答案

您需要使用所谓的元类。

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 中引入的虚拟构造函数。为了通过元类实例化调用适当的派生构造函数,这是必要的。

关于function - 将继承的帧作为参数传递给过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59561815/

相关文章:

php - SilverStripe 3 过滤/过滤掉函数中的数据对象

c++ - -1.#IND 是哪种 C++ 常量/宏?

C:函数未返回正确的值

delphi - FastMM SVN版本

json - Delphi 11.3 - 无法从变体创建/添加 JSON

c++ - 使用类参数化函数并访问类成员

Java继承: How to declare a static variable in Super and instantiate it in subclass

php - 将数组作为类构造函数的函数参数传递

c# - 是否每个用户定义的类都需要实现 IDisposable 接口(interface)来收集垃圾

delphi - 服务处的 LogonUser + CreateProcessAsUser = 错误 1314