Class-reference types 类引用类型--快要失传的技术
先摘一段原版的说明:
A class-reference type, sometimes called a metaclass, is denoted by a construction of the form
class of type
where type is any class type. The identifier type itself denotes a value whose type is class of type. If type1 is an ancestor of type2, then class of type2 is assignment-compatible with class of type1. Thus
type TClass = class of TObject;
var AnyObj: TClass;
declares a variable called AnyObj that can hold a reference to any class. (The definition of a class-reference type cannot occur directly in a variable declaration or parameter list.) You can assign the value nil to a variable of any class-reference type.
To see how class-reference types are used, look at the declaration of the constructor for TCollection (in the Classes unit):
type TCollectionItemClass = class of TCollectionItem;
...
constructor Create(ItemClass: TCollectionItemClass);
This declaration says that to create a TCollection instance object, you must pass to the constructor the name of a class descending from TCollectionItem.
Class-reference types are useful when you want to invoke a class method or virtual constructor on a class or object whose actual type is unknown at compile time.
当你想要在编译时调用一个类或对象的未知具体(真实)类型中的一个类方法或构造器函数时,类引用类型是很有用的。
光看帮助你大概搞不清楚这个有什么用。我举一个例子,一般mainform都有很多菜单按钮,用来打开不同的窗口,通常做法要在uses部分添加所有要引用的单元,十分麻烦,用上面的技术就可以避免引用。假设所有的业务窗口都从TAppBasicForm继承,你可以声明这样的类型:
TTAppBasicFormClass = class of TTAppBasicForm;
然后在每个业务窗口代码结尾处加上:
initialization
RegisterClass(TBusMemberNewForm); //TBusMemberNewForm从TAppBasicForm继承
finalization
UnRegisterClass(TBusMemberNewForm);
最后在Mainform用下面的函数:
procedure TTMainForm.ShowForm(sFormClass: string);
var
AppFormClass: TTAppBasicFormClass;
begin
try
AppFormClass := TTAppBasicFormClass(FindClass(sFormClass));
with AppFormClass.Create(self) do begin
Show;
end;
except
ShowMessage('Class ‘+sFormClass+' not exist or not register!');
end;
end;
这个函数的参数就是要打开的窗口类名
更进一步,因为项目中Menu的hint属性不会用到,可以用来存储要打开的类名,如下:
procedure TTMainForm.MainFormMenuClick(Sender: TObject);
var
sFormName: string;
begin
with sender as TMenuItem do begin
sFormName := Trim(Hint);
if sFormName<>'' then ShowForm(sFormName);
end;
end;
procedure TTMainFaceForm.SetMenuAction;
var
i: integer;
begin
for i:=0 to ComponentCount-1 do begin
if Components[i] is TMenuItem then begin
with Components[i] as TMenuItem do
if Trim(Hint)<>'' then OnClick := MainFormMenuClick;
end;
end;
end;
这样的话每增加一个菜单,只要指定菜单的hint属性就自动实现打开对应业务的功能,避免引用单元,也不用写菜单的onclick代码,非常简洁。当然这个还用到了RegisterClass和FindClass的技术,去看帮助就明白了。
Class-reference types 类引用类型--快要失传的技术的更多相关文章
- iOS: 学习笔记, 值与引用类型(译自: https://developer.apple.com/swift/blog/ Aug 15, 2014 Value and Reference Types)
值和引用类型 Value and Reference Types 在Swift中,有两种数据类型. 一是"值类型"(value type), 它是每一个实例都保存有各自的数据,通常 ...
- Delphi 类引用 Class Reference 元类 MetaClass 用法
delphi中类引用的使用实例 类引用类引用(Class Reference)是一种数据类型,有时又称为元类(MetaClass),是类的类型的引用.类引用的定义形式如下: class of type ...
- 【译】尝试使用Nullable Reference Types
随着.NET Core 3.0 Preview 7的发布,C#8.0已被认为是“功能完整”的.这意味着它们的最大亮点Nullable Reference Types,在行为方面也被锁定在.NET Co ...
- 《javascript高级程序设计》第五章 reference types
第5 章 引用类型5.1 Object 类型5.2 Array 类型 5.2.1 检测数组 5.2.2 转换方法 5.2.3 栈方法 5.2.4 队列方法 5.2.5 重排序方法 5.2.6 操作方法 ...
- swift语言点评十-Value and Reference Types
结论:value是拷贝,Reference是引用 Value and Reference Types Types in Swift fall into one of two categories: f ...
- 初探 C# 8 的 Nullable Reference Types
溫馨提醒:本文提及的 C# 8 新功能雖已通過提案,但不代表將來 C# 8 正式發布時一定會納入.這表示我這篇筆記有可能白寫了,也表示您不必急著瞭解這項新功能的所有細節,可能只要瞄一下底下的「概要」說 ...
- C++ 类的动态组件化技术
序言: N年前,我们曾在软件开发上出现了这样的困惑,用VC开发COM组件过于复杂,用VB开发COM组件发现效率低,而且不能实现面向对象的很多特性,例如,继承,多态等.更况且如何快速封装利用历史遗留的大 ...
- Nullable Reference Types 可空引用类型
在写C#代码的时候,你可能经常会遇到这个错误: 但如果想避免NullReferenceException的发生,确实需要做很多麻烦的工作. 可空引用类型 Null Reference Type 所以, ...
- C# 8 - Nullable Reference Types 可空引用类型
在写C#代码的时候,你可能经常会遇到这个错误: 但如果想避免NullReferenceException的发生,确实需要做很多麻烦的工作. 可空引用类型 Null Reference Type 所以, ...
随机推荐
- 【TensorFlow学习笔记 】name_socpe variable_scope
[引言]TensorFlow中的命名域是非常重要的概念,涉及到参数共享,方便命名参数管理,定义图结构 本文主要介绍name_scope 和 variable_scope,slim包中的arg_scop ...
- sso CAS
sso:single sign on,在多个应用系统中,用户只需要登陆一次就可以访问所有相互信任的应用系统 CAS框架:Central Authentication Service是实现sso单点登录 ...
- 第8章 传输层(1)_TCP/UDP协议的应用场景
1. 传输层的两个协议 1.1 TCP和UDP协议的应用场景 (1)TCP协议:如果要传输的内容比较多,需要将发送的内容分成多个数据包发送.这就要求在传输层用TCP协议,在发送方和接收方建立连接,实现 ...
- 关于-RegExp
// exec() exec() 查找并返回当前的匹配结果,并以数组的形式返回. // test() test() 方法检索字符串中的指定值.返回值是 true 或 false. // compile ...
- Windows 2016 无域故障转移群集部署方法 超详细图文教程 (一)
故障转移群集是一个很实用的功能,而windows在2016版本开始,终于支持不用域做故障转移群集. 在群集中,我们可以设定一个"群集IP" 而客户端只需要根据这个"群集I ...
- selenuim和phantonJs处理网页动态加载数据的爬取
一.图片懒加载 什么是图片懒加载? 案例分析:抓取站长素材http://sc.chinaz.com/中的图片数据 #!/usr/bin/env python # -*- coding:utf-8 -* ...
- Android标题头滑动渐变,Titlebar滑动渐变,仿美团饿了么标题头渐变;
原理就是滑动中改变透明度: 核心代码: rv.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public vo ...
- day6需要记忆(元组字典集合)
一:基本使用:(元组 tuple)优先掌握的操作1.按索引取值(正向取+反向取):只能取2.切片(顾头不顾尾,步长)3.长度 len()4.成员运算in和not in5.循环需要掌握的操作1.cou ...
- WPF圆角按钮与触发颜色变化
<Button x:Name="button1" Content="按钮1" Margin="10,10,0,0" Cursor=&q ...
- 重新指派usb转串口模块在linux系统中的设备调用名称
How to remap /dev/ttyUSB* to a specific name to be called by my program. How to map /dev/ttyUSB* to ...