先摘一段原版的说明:

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 类引用类型--快要失传的技术的更多相关文章

  1. iOS: 学习笔记, 值与引用类型(译自: https://developer.apple.com/swift/blog/ Aug 15, 2014 Value and Reference Types)

    值和引用类型 Value and Reference Types 在Swift中,有两种数据类型. 一是"值类型"(value type), 它是每一个实例都保存有各自的数据,通常 ...

  2. Delphi 类引用 Class Reference 元类 MetaClass 用法

    delphi中类引用的使用实例 类引用类引用(Class Reference)是一种数据类型,有时又称为元类(MetaClass),是类的类型的引用.类引用的定义形式如下: class of type ...

  3. 【译】尝试使用Nullable Reference Types

    随着.NET Core 3.0 Preview 7的发布,C#8.0已被认为是“功能完整”的.这意味着它们的最大亮点Nullable Reference Types,在行为方面也被锁定在.NET Co ...

  4. 《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 操作方法 ...

  5. swift语言点评十-Value and Reference Types

    结论:value是拷贝,Reference是引用 Value and Reference Types Types in Swift fall into one of two categories: f ...

  6. 初探 C# 8 的 Nullable Reference Types

    溫馨提醒:本文提及的 C# 8 新功能雖已通過提案,但不代表將來 C# 8 正式發布時一定會納入.這表示我這篇筆記有可能白寫了,也表示您不必急著瞭解這項新功能的所有細節,可能只要瞄一下底下的「概要」說 ...

  7. C++ 类的动态组件化技术

    序言: N年前,我们曾在软件开发上出现了这样的困惑,用VC开发COM组件过于复杂,用VB开发COM组件发现效率低,而且不能实现面向对象的很多特性,例如,继承,多态等.更况且如何快速封装利用历史遗留的大 ...

  8. Nullable Reference Types 可空引用类型

    在写C#代码的时候,你可能经常会遇到这个错误: 但如果想避免NullReferenceException的发生,确实需要做很多麻烦的工作. 可空引用类型 Null Reference Type 所以, ...

  9. C# 8 - Nullable Reference Types 可空引用类型

    在写C#代码的时候,你可能经常会遇到这个错误: 但如果想避免NullReferenceException的发生,确实需要做很多麻烦的工作. 可空引用类型 Null Reference Type 所以, ...

随机推荐

  1. 将本地代码提交至gitHub

    1.注册github账号 2.本地安装git 3.打开需要提交代码的目录 4.右击git bash here 5. $ git init 6  $ ssh-keygen -t rsa -C " ...

  2. (转)CLR20R3 程序终止的几种解决方案

    原文地址:https://blog.csdn.net/fxfeixue/article/details/4466899 这是因为.NET Framework 1.0 和 1.1 这两个版本对许多未处理 ...

  3. cocos子节点转父节点坐标 原理浅析(局部坐标转世界坐标同理)

    在CCNode的类中,有一个得到 一个节点坐标系转换父亲坐标系的一个矩阵,节点内坐标乘以这个矩阵,就可以转换为在节点父节点中的坐标,方法名为: Mat4& Node::getNodeToPar ...

  4. 20165205 2017-2018-2 《Java程序设计》第六周学习总结

    20165205 2017-2018-2 <Java程序设计>第六周学习总结 教材学习内容总结 String类 String对象(常量,对象) 字符串并置(结果仍是常量) 常用方法 len ...

  5. 【HQL】常用函数

    CONCAT_WS(separator, str1, str2,...) 多列转1列,以分割符分割 使用场景: 1.多列在一列显示: 2.多列转多行作为辅助,结合split和explode使用 SEL ...

  6. 8.2.1-优化SELECT语句

    8.2.1.优化 SELECT 语句 由SELECT 语句组成的查询,在数据中执行所有的查询.对这类语句的调优排在首位,无论是调优动态web网页的二级响应时间,还是减少生成巨大隔夜报告的时间. 而且, ...

  7. tkinter events format

    tkinter label 标签主要显示,通常不与用户进行交互事件 frame容器上获取点击的事件坐标 event.x,event.y event.key获取键盘数据

  8. js:浏览器插件

    1.chrome background.js //chrome.webRequest.onBeforeRequest.addListener(function(info) { // chrome.ta ...

  9. 《算法》第三章部分程序 part 3

    ▶ 书中第三章部分程序,加上自己补充的代码,红黑树 ● 红黑树,大部分方法与注释与二叉树相同 package package01; import java.util.NoSuchElementExce ...

  10. odps 使用参考 & tips

    1.  自定义udf 编写udf 1)pom.xml <dependency> <groupId>com.aliyun.odps</groupId> <art ...