delphi 泛型之 动态创建类的对象

本文探讨了Delphi泛型在实际使用中与C++类模板的差异,尤其是类模板对象创建的复杂性。通过示例,展示了如何在Delphi中巧妙地实现动态创建类模板的对象。
摘要由CSDN通过智能技术生成

delphi 泛型使用很是折腾,和c++的差得太远。特别是类模板的使用,创建很是麻烦,还好可以变通的写出来。

以下举个例子,如何使用类模板。

unit test1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, 
Vcl.Dialogs, Generics.Collections, Vcl.StdCtrls, Vcl.Buttons;

type
  TForm4 = class(TForm)
    btn1: TBitBtn;
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;


  TValue = class  //基类
  public
    function Name: string; virtual;  //虚方法
  end;

  TValue1= class(TValue)
  public
    function Name: string; override;  //覆盖基类虚方法
  end;

  TTest = class
  public
    procedure Method<T: TValue>;  //测试方法1,用基类做模板类型,可以传入任意子类
    procedure Method2<T: class>;  //测试方法2,用class关键字,可以传入任意类
  end;


var
  Form4: TForm4;

implementation

{$R *.dfm}


{ TV
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值