先摘一段原版的说明:

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. 第8课 列表初始化(3)_防止类型收窄、explicit关键字

    1. 防止类型收窄 (1)类型收窄:指的是导致数据内容发生变化或者精度丢失的隐式类型转换. (2)类型收窄的几种情况: ①从浮点数隐式转换为整型数,如int i=2.2; ②从高精度浮点数隐式转换为低 ...

  2. async方法:async+await

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. Hive 2.1.1安装配置

    ##前期工作 安装JDK 安装Hadoop 安装MySQL ##安装Hive ###下载Hive安装包 可以从 Apache 其中一个镜像站点中下载最新稳定版的 Hive, apache-hive-2 ...

  4. Python之网络编程(Socket)

    1.网络通信原理与互联网协议 详见:https://www.cnblogs.com/JackLi07/p/9218039.html 2.socket层 以上是tcp/ip五层协议的结构图,我们没有看到 ...

  5. C# .NET Web Api 保存Session

    Global.asax中添加: public override void Init() { this.PostAuthenticateRequest += (sender, e) => Http ...

  6. httpd-2.4.6

    1.基础 安装: [root@tri manual]# yum install httpd-manual httpd 源码编译: configure配置选项 配置选项 默认值 备注 -prefix   ...

  7. spring初始化相关

    获取applicationContext implements ApplicationContextAware @Override public void setApplicationContext( ...

  8. oracle的schema的含义

    转自:http://www.cnblogs.com/sfmjp/articles/2932748.html 在现在做的Kraft Catalyst 项目中,Cransoft其中有一个功能就是schem ...

  9. oracle 多行合并为一行

    sys_connect_by_path select i,ltrim(max(sys_connect_by_path(a,',')),',') afrom(select i,a,d,min(d) ov ...

  10. SecureCRT连接linux,Hive中无法使用删除键