zoukankan      html  css  js  c++  java
  • delphi 类 线程 反射 泛型 简单理解

      1 unit Unit3;
      2 
      3 interface
      4 
      5 uses
      6   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
      7   Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Rtti;
      8 
      9 type
     10   TForm3 = class(TForm)
     11     Button1: TButton;
     12     Edit1: TEdit;
     13     Button2: TButton;
     14     Edit2: TEdit;
     15     Button3: TButton;
     16     Button4: TButton;
     17     procedure Button1Click(Sender: TObject);
     18     procedure Button2Click(Sender: TObject);
     19     procedure Button3Click(Sender: TObject);
     20     procedure Button4Click(Sender: TObject);
     21   private
     22     { Private declarations }
     23   public
     24     { Public declarations }
     25   end;
     26 
     27 type
     28   Tmyclass = class     //
     29   public
     30     constructor Create(tempname: string); overload;
     31   private
     32     myname: string;
     33     type
     34       mytype = (jack, mike, tom);
     35   end;
     36 
     37 type
     38   Tmythread = class(TThread)  //线程
     39   protected
     40     procedure Execute; override;
     41   private
     42     procedure myrun;
     43   public
     44     constructor Create; overload;
     45   end;
     46 
     47 type
     48   TmyKV<T> = class          //泛型
     49   private
     50     Fkey: string;
     51     Fvalue: T;
     52     procedure SetFkey(const Value: string);
     53     procedure SetFvalue(const Value: T);
     54   public
     55     property PFkey: string read Fkey write SetFkey;
     56     property PFvalue: T read Fvalue write SetFvalue;
     57   end;
     58 
     59 var
     60   Form3: TForm3;
     61 
     62 implementation
     63 
     64 {$R *.dfm}
     65 
     66 procedure TForm3.Button1Click(Sender: TObject);
     67 var
     68   my1: Tmyclass;
     69 begin
     70   my1 := Tmyclass.Create('Jacky');
     71   try
     72     Edit1.Clear;
     73     Edit1.Text := my1.myname;
     74   finally
     75     my1.Destroy;
     76   end;
     77 end;
     78 
     79 { Tmyclass }
     80 
     81 constructor Tmyclass.Create(tempname: string);
     82 begin
     83   inherited Create();
     84   myname := tempname;
     85 end;
     86 
     87 procedure TForm3.Button2Click(Sender: TObject);    //反射
     88 var
     89   rtx: TRttiContext;
     90   rt: TRttiType;
     91   f: TRttiField;
     92   mycls: Tmyclass;
     93 begin
     94   mycls := Tmyclass.Create('Tom');
     95 
     96   rt := rtx.GetType(Tmyclass);
     97   f := rt.GetField('myname');
     98   Edit1.Text := f.GetValue(mycls).AsString;
     99   mycls.Free;
    100 end;
    101 
    102 procedure TForm3.Button3Click(Sender: TObject); //线程
    103 var
    104   mythread: Tmythread;
    105 begin
    106   mythread := Tmythread.Create;
    107   mythread.Resume;
    108 end;
    109 
    110 procedure TForm3.Button4Click(Sender: TObject);  //泛型
    111 var
    112   myKV: TmyKV<Tmyclass>;
    113   myll: Tmyclass;
    114 begin
    115   myll := Tmyclass.Create('汤姆');
    116   myKV := TmyKV<Tmyclass>.Create;
    117   myKV.PFkey := 'tom';
    118   myKV.PFvalue := myll;
    119   Edit2.Text := myKV.PFvalue.myname;
    120   myKV.Free;
    121   myll.free;
    122 end;
    123 
    124 { Tmythread }
    125 
    126 constructor Tmythread.Create;
    127 begin
    128   inherited;
    129   FreeOnTerminate := True;
    130 end;
    131 
    132 procedure Tmythread.Execute;
    133 begin
    134   inherited;
    135   myrun;
    136 end;
    137 
    138 procedure Tmythread.myrun;
    139 var
    140   i: Integer;
    141 begin
    142   for i := 1000 downto 1 do
    143   begin
    144     form3.Edit1.Text := IntToStr(i);
    145     Sleep(500);
    146   end;
    147 
    148 end;
    149 
    150 { TmyKV<T> }
    151 
    152 procedure TmyKV<T>.SetFkey(const Value: string);
    153 begin
    154   Fkey := Value;
    155 end;
    156 
    157 procedure TmyKV<T>.SetFvalue(const Value: T);
    158 begin
    159   Fvalue := Value;
    160 end;
  • 相关阅读:
    配置ssl(阿里云+腾讯云)
    使用Nativefier将web页面打包为桌面应用
    临时切换淘宝源下载包
    ajax实现异步上传多图并且预览
    通过phpexcel插件导出报表功能实现
    ThinkPHP5.0自定义命令行的使用
    Longest Substring Without Repeating Characters
    计算机操作系统巡回置换算法
    HDU 2041 DP
    HDU 2044 DP (fibonacci)
  • 原文地址:https://www.cnblogs.com/yangxuming/p/10535867.html
Copyright © 2011-2022 走看看