unit Unit1;

 interface

 uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, jpeg, ExtCtrls,
IniFiles;//TStringHash使用 type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Image1: TImage;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; type
// 边信息基类接口
IClassInfo = interface
['{219E2CE1-78AE-4479-A4D8-A23DB421621C}']
function GetName: string;
end;
//
TInfoClass = class of TMyClass;
//自定义的类
TMyClass = class(TObject, IInterface)
protected
FRefCount: Integer;
private
FMyName: string;
public
function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
constructor Create; virtual;
function GetName: string; virtual;
property RefCount: Integer read FRefCount;
end;
//展示类
TMyClass1 = class(TMyClass, IClassInfo)
private
FMyName: string;
public
constructor Create; override;
function GetName: string; override;
end;
//工厂类
TClassInfoFactroy = class
class function CreateClass(const ATypeID: Integer): TMyClass;
end; var
Form1: TForm1; implementation {$R *.dfm} var
MyClassList: TStringHash; { TMyClass }
//注册类方法
procedure RegisterMyClass(const ATypeID: Integer; AClass: TInfoClass);
var
num: Integer;
begin
num := MyClassList.ValueOf(IntToStr(ATypeID));
if num = - then
MyClassList.Add(IntToStr(ATypeID), Integer(AClass))
else
Assert(False, Format('ElementTypeID %d ''s EdgeInfo already registered', [ATypeID]));
end; constructor TMyClass.Create;
begin
// 子类实现
end; function TMyClass.GetName: string;
begin
// 子类实现
end; function TMyClass.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
if GetInterface(IID, Obj) then
Result :=
else Result := E_NOINTERFACE;
end; function TMyClass._AddRef: Integer;
begin
Result := InterlockedIncrement(FRefCount);
end; function TMyClass._Release: Integer;
begin
Result := InterlockedDecrement(FRefCount);
if Result = then Destroy;
end; { TMyClass1 } constructor TMyClass1.Create;
begin
FMyName := '类1';
end; function TMyClass1.GetName: string;
begin
Result := FMyName;
end; { TClassInfoFactroy }
//建立类方法
class function TClassInfoFactroy.CreateClass(const ATypeID: Integer): TMyClass;
var
nClass: Integer;
begin
nClass := MyClassList.ValueOf(IntToStr(ATypeID));
if nClass <> - then
Result := TInfoClass(nClass).Create
else begin
Result := nil;
end;
end;
//调用类方法
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.Text := (TClassInfoFactroy.CreateClass() as IClassInfo).GetName;
end;
//初始化
initialization
if MyClassList = nil then
MyClassList:= TStringHash.Create();
// 注册类
RegisterMyClass(, TMyClass1); end.

Delphi使用TStringHash实现建立类(有点像反射)的更多相关文章

  1. php 建立类POST/GET 的HTTP请求

    1.第一种利用fsock的方式来建立类POST的请求. <?php $srv_ip = '192.168.1.5';//你的目标服务地址. $srv_port = 80;//端口 $url = ...

  2. Delphi之TStrings和TStringLists类

    Delphi之TStrings和TStringLists类 有些类不是组件,但它们支持存在的组件.这些类是其他组件的典型属性,直接由TPersistent派生,如TStrings.TCanvas和TC ...

  3. delphi 控制音量 静音的类

    delphi 控制音量 静音的类 unit ttSound; interface uses winapi.windows, winapi.Messages; type SimpleSoundContr ...

  4. Delphi 跨单元进入(访问)类的私有成员,protected ,private部分

    http://blog.sina.com.cn/s/blog_5f8861b60102v1nl.html Delphi 跨单元进入(访问)类的私有成员,protected ,private部分 (20 ...

  5. C# 直接创建多个类和使用反射创建类的性能

    原文:C# 直接创建多个类和使用反射创建类的性能 本文告诉大家我对比的使用直接创建多个类和使用反射创建多个类的性能 在上一篇 C# 程序内的类数量对程序启动的影响 的基础上,继续做实验 现在创建 10 ...

  6. 2019-11-29-C#-直接创建多个类和使用反射创建类的性能

    原文:2019-11-29-C#-直接创建多个类和使用反射创建类的性能 title author date CreateTime categories C# 直接创建多个类和使用反射创建类的性能 li ...

  7. 2019-8-31-C#-直接创建多个类和使用反射创建类的性能

    title author date CreateTime categories C# 直接创建多个类和使用反射创建类的性能 lindexi 2019-08-31 16:55:58 +0800 2018 ...

  8. 044-PHP获得多个类对应的反射信息

    <?php //获得多个类对应的反射信息 class demo{ public $str_1; private $str_2; protected $str_3; public function ...

  9. 043-PHP简单获得一个类对应的反射信息

    <?php // 简单获得一个类对应的反射信息 class demo{ CONST CON_STR = '123456'; public $str_1; private $str_2; prot ...

随机推荐

  1. poi操作oracle数据库导出excel文件2

    package com.test; import java.io.File;  import java.io.FileInputStream;  import java.io.FileNotFound ...

  2. eclipse的安装

    eclipse的安装只要记住一条就好,那就是eclipse安装的时候版本要一致(就是,安装eclipse 32位的,同时要配套jdk 32位,tomcat 32位的),同理64位的也是一样的. 本人安 ...

  3. TeeChart的X轴,使用伪装的时间

    TeeChart曲线的X轴是时间,但是频率很高.没法完全显示. 例如,一秒钟有2000个点,那么点与点的间隔为0.5毫秒. 使用TChart类的GetAxisLabel事件, 函数手册上对此事件的解释 ...

  4. Android Things:Raspberry Pi 3 B 刷入 Android Things

    参考文章: http://www.andtuts.com/a-beginners-guide-to-raspberry-pi-3-b-and-android-things/?utm_source=An ...

  5. Codeforces Round #207 (Div. 2)C

    读错题意了..线段树延迟标记 白刷这么多线段树 #include <iostream> #include<cstdio> #include<cstring> #in ...

  6. linux delete files older than 3 days

    4 down vote accepted This is easy enough (although note that this goes by a modification time more t ...

  7. xcode6.3 编译ffmpeg 2.6.3(已验证编译成功)

    1.解压ffmpeg2.6.3源代码,在根目录下新建文件myconfig,内容如下,执行命令chmod 777 ./myconfig 2../myconfig 3.make 4.make instal ...

  8. LT1619EMS8 锂电池 升压电路分析

    LT1619EMS8 锂电池 升压电路分析 本文主要是分析LT1619EMSB锂电池升压芯片电路,知道其大致是怎么工作的,其中的一些电阻该如何配置. 2016-1-23 深圳 南山平山村 曾剑锋 一. ...

  9. ByteBuffer用法小结

    在NIO中,数据的读写操作始终是与缓冲区相关联的.读取时信道(SocketChannel)将数据读入缓冲区,写入时首先要将发送的数据按顺序填入缓冲区.缓冲区是定长的,基本上它只是一个列表,它的所有元素 ...

  10. Zend Framework 入门(2)—多国语言支持

    如果你的项目想要支持多语言版本,那么就需要用到 Zend_Translate.Zend_Translate 的详细文档在这里,不过如果想偷懒的话,也很简单,在View Helpers 文档中介绍了如何 ...