zoukankan      html  css  js  c++  java
  • Delphi中的泛型

    -------------Delphi7没有泛型

    开发环境是Delphi XE  (是XE,不是XE2,不是XE10)

    -------------------

    --------------Unit 开始-

      1 unit Unit1;
      2 
      3 interface
      4 
      5 uses
      6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      7   Dialogs, StdCtrls, ExtCtrls, TypInfo;    //,Generics.Collections
      8 
      9 type
     10   TForm1 = class(TForm)
     11     Button1: TButton;
     12     Button2: TButton;
     13     Button3: TButton;
     14     Bevel1: TBevel;
     15     Button4: TButton;
     16     Button5: TButton;
     17     procedure Button1Click(Sender: TObject);
     18     procedure Button2Click(Sender: TObject);
     19     procedure Button3Click(Sender: TObject);
     20     procedure Button4Click(Sender: TObject);
     21     procedure Button5Click(Sender: TObject);
     22   private
     23     { Private declarations }
     24   public
     25     { Public declarations }
     26   end;
     27 
     28   Tmycalss=class
     29     private
     30       type
     31         TMyRecord=record
     32           s:string;
     33         end;
     34     public
     35       Myr:TmyreCord;
     36   end;
     37   {
     38    泛型:声明定义的时候看上去就是把类型当成形参一样的东西用T表示,当然也可以用其他的字符代替;
     39          在使用的时候把<T> 中的T看成是实参,实参是类型
     40    比如:
     41    var
     42      ss:TMygeneric<String> ;
     43   }
     44   TMygeneric<T> =array[0..9] of  T ; //TMygeneric<sss> =array[0..9] of  sss;这个也可以的
     45 
     46   TmyGenericClass<T>=class(Tobject)
     47     private
     48       FIDCode:T;
     49     public
     50   end;
     51 
     52   TmyGenericClass_A<T:Class> =class(TmyGenericClass<T>)
     53     private
     54       FIDCode_A:T;
     55   end;
     56 var
     57   Form1: TForm1;
     58 
     59 implementation
     60 
     61 {$R *.dfm}
     62 
     63 procedure TForm1.Button1Click(Sender: TObject);
     64 var
     65   ss:TMygeneric<String>;     //泛型
     66   i:Integer;
     67 begin
     68   for  i:=0 to 9 do
     69   begin
     70     ss[i]:=IntToStr(i);
     71   end;
     72 end;
     73 
     74 procedure TForm1.Button2Click(Sender: TObject);
     75 var
     76   Myc:Tmycalss;
     77 begin
     78   Myc:=Tmycalss.Create;
     79   Myc.Myr.s:='8888';
     80   ShowMessage(Myc.Myr.s);  //看下类中类
     81   FreeAndNil(Myc);
     82 end;
     83 
     84 procedure TForm1.Button3Click(Sender: TObject);
     85 var
     86   MyArrayc:TMygeneric<Tmycalss>;  //泛型
     87   i:Integer;
     88 begin
     89   for  i:=0 to 9 do
     90   begin
     91     MyArrayc[i]:=Tmycalss.Create;
     92     MyArrayc[i].Myr.s:='498789';
     93   end;
     94   for  i:=0 to 9 do
     95   begin
     96     FreeAndNil(MyArrayc[i]);
     97   end;
     98 end;
     99 
    100 procedure TForm1.Button4Click(Sender: TObject);
    101 var
    102   vClass:TmyGenericClass<String>;
    103 begin
    104   vClass:=TmyGenericClass<String>.Create;
    105   vClass.FIDCode:='978978';
    106   ShowMessage(vClass.FIDCode );
    107   FreeAndNil(vClass);
    108 end;
    109 
    110 procedure TForm1.Button5Click(Sender: TObject);
    111 var
    112   vClass_A:TmyGenericClass_A<Tmycalss> ;
    113 begin
    114   vClass_A:=TmyGenericClass_A<Tmycalss>.Create;
    115   vClass_A.FIDCode_A:=Tmycalss.Create;
    116   vClass_A.FIDCode_A.Myr.s:='456465';
    117   ShowMessage(vClass_A.FIDCode_A.Myr.s);
    118   FreeAndNil(vClass_A.FIDCode_A);
    119   FreeAndNil(vClass_A);
    120 end;
    121 
    122 end.

    -------------Unit结束-----

    --------Form 开始--------

     1 object Form1: TForm1
     2   Left = 754
     3   Top = 463
     4   BorderStyle = bsDialog
     5   Caption = 'Form1'
     6   ClientHeight = 185
     7   ClientWidth = 308
     8   Color = clBtnFace
     9   Font.Charset = DEFAULT_CHARSET
    10   Font.Color = clWindowText
    11   Font.Height = -11
    12   Font.Name = 'Tahoma'
    13   Font.Style = []
    14   OldCreateOrder = False
    15   Position = poDesigned
    16   PixelsPerInch = 96
    17   TextHeight = 13
    18   object Bevel1: TBevel
    19     Left = 152
    20     Top = 8
    21     Width = 9
    22     Height = 159
    23   end
    24   object Button1: TButton
    25     Left = 48
    26     Top = 24
    27     Width = 75
    28     Height = 25
    29     Caption = 'Button1'
    30     TabOrder = 0
    31     OnClick = Button1Click
    32   end
    33   object Button2: TButton
    34     Left = 48
    35     Top = 72
    36     Width = 75
    37     Height = 25
    38     Caption = 'Button2'
    39     TabOrder = 1
    40     OnClick = Button2Click
    41   end
    42   object Button3: TButton
    43     Left = 48
    44     Top = 119
    45     Width = 75
    46     Height = 25
    47     Caption = 'Button3'
    48     TabOrder = 2
    49     OnClick = Button3Click
    50   end
    51   object Button4: TButton
    52     Left = 192
    53     Top = 24
    54     Width = 75
    55     Height = 25
    56     Caption = 'Button4'
    57     TabOrder = 3
    58     OnClick = Button4Click
    59   end
    60   object Button5: TButton
    61     Left = 192
    62     Top = 64
    63     Width = 75
    64     Height = 25
    65     Caption = 'Button5'
    66     TabOrder = 4
    67     OnClick = Button5Click
    68   end
    69 end

    -------Form 结束---------

  • 相关阅读:
    Django 模版当中使用中文 UnicodeDecodeError at / 问题
    sql中索引不会被用到的几种情况
    Django安装
    分享一个webapi接口性能测试的工具
    orleans 2.0 进阶之自定义持久化储存
    centos7 .net core 使用supervisor守护进程,可以后台运行
    将.net core 发布到Linux上的一些坑
    .net core 部署在Linux系统上运行的环境搭建总结
    .net core 基于NPOI 的excel导入导出类,支持自定义导出哪些字段,和判断导入是否有失败的记录
    基于Ace Admin 的菜单栏实现
  • 原文地址:https://www.cnblogs.com/dmqhjp/p/15034422.html
Copyright © 2011-2022 走看看