Delphi中的泛型-------------Delphi7没有泛型
开发环境是Delphi XE (是XE,不是XE2,不是XE10)
-------------------
--------------Unit 开始-
1unit Unit1;
2
3interface
4
5us
6 Windows, Messages, SysUtils, Variants, Class, Graphics, Controls, Forms,
7 Dialogs, StdCtrls, ExtCtrls, TypInfo; //,Generics.Collections
8
9type
10 TForm1 = class(TForm)
11 Button1: TButton;
12 Button2: TButton;
13 Button3: TButton;
14 Bevel1: TBevel;
15 Button4: TButton;
16 Button5: TButton;
17procedure Button1Click(Sender: TObject);
18procedure Button2Click(Sender: TObject);
19procedure Button3Click(Sender: TObject);
20procedure Button4Click(Sender: TObject);
21procedure Button5Click(Sender: TObject);
22private
23{ Private declarations }
24public
25{ Public declarations }
26end;
27
28 Tmycalss=class
29private
30type
31 TMyRecord=record
32 s:string;
33end;
34public
35 Myr:TmyreCord;
36end;
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)
47private
48 FIDCode:T;
49public
50end;
51
52 TmyGenericClass_A<T:Class> =class(TmyGenericClass<T>)
53private
54 FIDCode_A:T;
55end;
56var
57 Form1: TForm1;
58
59implementation
60
61{$R *.dfm}
62
63procedure TForm1.Button1Click(Sender: TObject);
64var
责有攸归
65 ss:TMygeneric<String>; //泛型dns刷新
66 i:Integer;
67begin
68for i:=0to9do
69begin
70 ss[i]:=IntToStr(i);
71end;
72end;
73
74procedure TForm1.Button2Click(Sender: TObject);
75var
76 Myc:Tmycalss;
77begin
78 Myc:=Tmycalss.Create;
79 Myc.Myr.s:='8888';
80 ShowMessage(Myc.Myr.s); //看下类中类
81FreeAndNil(Myc);
82end;
83
84procedure TForm1.Button3Click(Sender: TObject);
85var
86 MyArrayc:TMygeneric<Tmycalss>; //泛型春节歇后语
87 i:Integer;
88begin
89for i:=0to9do
90begin
91 MyArrayc[i]:=Tmycalss.Create;
92 MyArrayc[i].Myr.s:='498789';
93end;
94for i:=0to9do
95begin
96FreeAndNil(MyArrayc[i]);
97end;
98end;
99
100procedure TForm1.Button4Click(Sender: TObject); 101var
102 vClass:TmyGenericClass<String>;
103begin
104 vClass:=TmyGenericClass<String>.Create;
105 vClass.FIDCode:='978978';
106 ShowMessage(vClass.FIDCode );
107FreeAndNil(vClass);
108end;
梅花有什么作用
109
110procedure TForm1.Button5Click(Sender: TObject); 111var
112 vClass_A:TmyGenericClass_A<Tmycalss> ;
113begin
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);
118FreeAndNil(vClass_A.FIDCode_A);
119FreeAndNil(vClass_A);
120end;
121
122end.
-------------Unit结束-----
--------Form 开始--------
1object 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.Chart = DEFAULT_CHARSET
10 Font.Color = clWindowText
11 Font.Height = -11
12 Font.Name = 'Tahoma'
13 Font.Style = []
14 OldCreateOrder = Fal
15 Position = poDesigned
16 PixelsPerInch = 96
17 TextHeight = 13
18object Bevel1: TBevel
19 Left = 152
20 Top = 8
21 Width = 9
22 Height = 159
23end
24object Button1: TButton
25 Left = 48
26 Top = 24
27 Width = 75
28 Height = 25
29 Caption = 'Button1'
30 TabOrder = 0
31 OnClick = Button1Click 32end
33object Button2: TButton
34 Left = 48
35 Top = 72
36 Width = 75
37 Height = 25
38 Caption = 'Button2'
39 TabOrder = 1
周长计算方法
40 OnClick = Button2Click 41end
42object Button3: TButton
43 Left = 48
44 Top = 119
剖腹产过程
45 Width = 75
46 Height = 25
47 Caption = 'Button3'
48 TabOrder = 2
49 OnClick = Button3Click 50end
51object Button4: TButton
52 Left = 192
53 Top = 24
54 Width = 75
55 Height = 25
56 Caption = 'Button4'
57 TabOrder = 3
58 OnClick = Button4Click 59end
60object Button5: TButton
61 Left = 192
62 Top = 64
63 Width = 75
64 Height = 25
65 Caption = 'Button5'
北京物资学院邮编66 TabOrder = 4
67 OnClick = Button5Click 68end
69end
-------Form 结束---------