|
|
|
理解类引用这种类型
作者:Webmaster 来源:Linuxdby.com 点击:
日期:2007-06-05
[收藏] [投稿]
IE是否经常中毒?推荐您
|
问题: 书中写到:
--------------------
类的引用是一种类型,它不是对对象的引用,也不是对类的引用,
而是对类的类型的引用。类的引用类型定义了引用变量的类型。
假设用户定义了一个类:
type
CMyClass=class
...
end
下面定义一个新类的引用类型,与下面的类相关:
type
MyClassRef=calss of CMyClass
这样就声明了两个类型的变量:
aClassRef:MyClassRef;
aObject:CMyClass;
其中第一个变量引用一个对象,第二个引用类:
aClassRef=CMyClass;
aObject=CMyClass.Create;
--------------------
这一段我看不明白,请各位解释一下。谢谢
|
| 来自:Pipi., 时间:2002-2-8 23:13:00, ID:916427 |
另外举一个例子吧,比如我可以写一个函数
type TControlClass = class of TControl;
procedure insert_control(cls:TControlClass);
begin
obj;=cls.Create(Application);
把obj放进form
end;
调用的时候,如果是 insert_control(TEdit) 那么放进form的是一个编辑框
如果是 insert_control(TButton) 那么放进form的是一个按钮
谢复杂一点就是
var ctl:TControlClass;
ctr:=TEdit;
insert_control(ctl)
------------------------------
注意上面我们用到TButton和TEdit它们都有共同的祖先TControl
我们定义一个基类,然后通过传递不同的子类,那么一个函数只写一次就实现不同的效果 |
| 来自:Holyowl, 时间:2002-2-8 23:26:00, ID:916440 |
类引用的用途就是创建在编译器无法确定的对象,举个列子:
Type
TControlCls = Class of TControl;
function CreateComponent(ControlCls: TControlCls): TControl;
begin
result:=ControlCls.Create(Form1);
...
end;
调用时如:
CreateComponent(TMemo);//创建TMemo对象
CreateComponent(TEdit);//创建TEdit对象
|
| 来自:renrf, 时间:2002-2-9 0:07:00, ID:916478 |
谢谢,
我书上的例子怎样理解呢? |
| 来自:Pipi., 时间:2002-2-9 0:29:00, ID:916503 |
你书上的例子就更简单了,根本没讲到实际的用途
就是出现aClassRef的地方你把它看成CMyClass好了 |
| 来自:testnet, 时间:2002-2-16 1:17:00, ID:923102 |
提前提前,我还没看明
Type
TControlCls = Class of TControl;
function CreateComponent(ControlCls: TControlCls): TControl;
begin
result:=ControlCls.Create(Form1);
...
end;
function CreateComponent(ControlCls: TControl): TControl;
begin
result:=ControlCls.Create(Form1);
...
end;
上面这两个东东的结果不是一样吗?有会什么差别??我只注重结果
|
| 来自:Pipi., 时间:2002-2-16 1:26:00, ID:923112 |
当然不同了 |
| 来自:beta, 时间:2002-2-16 2:34:00, ID:923171 |
to testnet:
将你的两个函数名分别改为 CreateComponent1 和 CreateComponent2
你象这样编译一下:
CreateComponent1(TEdit);
CreateComponent2(TEdit);
看出 结果 上的差别了吧? |
|
|