`类的类型声明的含义是什么? [英] what is the meaning of `Class of ` type declaration?

查看:217
本文介绍了`类的类型声明的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



TMyObjectClass = TMyObject的类;

/ code>



我有点困惑,想知道这个语句的含义是什么。
TMyObjectClass 在语句之上没有声明。



TMyObject 的声明如下:



TMyObject = class(TObject)
private
一些私人成员声明
Public
//一些公共变量
end;



是语句的含义
TMyObjectClass = TMyObject类;



TMyObjectClass 工程?



我对Delphi有点新,所以请帮我了解这些类型的声明

解决方案

这是一个类参考



它们用于处理元类。规范的例子是Delphi流框架,它使用

  TComponentClass = TComponent类; 

这允许动态绑定到虚拟构造函数。 TComponent 构造函数是 virtual 。流式框架需要实例化从 TComponent 派生的类。它做了这样的事情:

  var 
ComponentClass:TComponentClass;
组件:TComponent;
....
ComponentClass:= GetComponentClassSomehowDoesntMatterHow;
组件:= ComponentClass.Create(Owner);

现在,因为 TComponent.Create code> virtual ,这是以多态方式绑定的。如果 TComponentClass TButton ,则会调用 TButton.Create 。如果 TComponentClass TPanel ,则会调用 TPanel.Create 。等等。



最重要的是要实现的是构造的类只在运行时才被确定。注意,许多语言缺乏这种能力,最值得注意的是C ++。


While going through one of my code, I am stuck on one statement which is as below.

TMyObjectClass = class of TMyObject;

I am a bit confused, and wondering what is the meaning of this statement. As TMyObjectClass has no declaration above the statement.

and TMyObject is having declaration as below:

TMyObject = class(TObject) private //some private member declaration Public // some public variables end;

So, my question is what is the meaning of the statement TMyObjectClass = class of TMyObject;

and How TMyObjectClass works?

I am a bit new to Delphi, so please help me to get some idea about these type of declaration and there workarounds.

解决方案

This is a Class Reference.

They are used to work with meta classes. The canonical example is the Delphi streaming framework which uses

TComponentClass = class of TComponent;

This allows for dynamic binding to virtual constructors. The TComponent constructor is virtual. The streaming framework needs to instantiate classes derived from TComponent. It does so something like this:

var
  ComponentClass: TComponentClass;
  Component: TComponent;
....
ComponentClass := GetComponentClassSomehowDoesntMatterHow;
Component := ComponentClass.Create(Owner);

Now, because TComponent.Create is virtual, this is bound in a polymorphic fashion. If TComponentClass is TButton, then TButton.Create is called. If TComponentClass is TPanel, then TPanel.Create is called. And so on.

The most important thing to realise is that the class that is constructed is determined only at runtime. Note that many languages lack this capability, most notably C++.

这篇关于`类的类型声明的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆