盼望大虾解释一个基础的问题:关于 class of ** 的问题
procedure OpenMDIFrm(FrmClass:TFormClass; var frm);
begin
if not Assigned(TForm(frm)) then
begin
TForm(frm):=TForm(FrmClass.NewInstance); //??
TForm(frm).Create(Application); //?? NewInstance & Create() 的区别
end;
TForm(frm).Show;
end;
问题点数:50、回复次数:6Top
1 楼searoom(海龙)回复于 2006-03-10 00:32:43 得分 0
Delphi 真的没落了?Top
2 楼mianbaosoft(不懂爱)(难道真的要回火星)回复于 2006-03-10 08:37:55 得分 5
class of 定义的是类的类,可以同对象的类一样理解,主要看楼主想干吗Top
3 楼cncharles(旺仔)回复于 2006-03-10 08:50:18 得分 10
把下面的英文看明白就知道为什么这样.
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.
NewInstance method
Allocates memory for an instance of an object type and returns a pointer to that new instance.
Delphi syntax:
class function NewInstance: TObject; virtual;
C++ syntax:
virtual TObject* __fastcall NewInstance(TClass cls);
Description
All constructors call NewInstance automatically. NewInstance calls InstanceSize to determine how much memory to allocate from the heap to contain a particular instance. Do not call NewInstance directly.
Override NewInstance only for special memory allocation requirements. For example, when allocating a large number of identical objects that all need to be in memory at the same time, you could allocate a single block of memory for the entire group, then override NewInstance to use part of that larger block for each instance.
If you override NewInstance to allocate memory, you may need to override FreeInstance to deallocate the memory.
Note: By default NewInstance calls InitInstance.
Top
4 楼GARNETT2183(KingWolves (http://kevin-lu.blogspot.com))回复于 2006-03-10 09:15:54 得分 15
类引用.也就是类的变量,可以类引用类型,变换不同的类...Top
5 楼wvins(逸岚)回复于 2006-03-10 10:26:35 得分 20
第一感觉和java中的class对象特别像
与c++中的typeinfo对象有些类似。
感觉和c++中的traits有些类似,traits是用来支持泛型编程的。
常见的应用就是通过class of xx的变量创建xx类型的变量。
var
x: class of TObject
x.create就是创建一个TObject对象。
感觉java借鉴了delphi很多东西,包括在堆上产生对象。
java的class对象应该是从delphi中借鉴过去,完成同样的功能。
应该说能够实现一定的泛型编程,但目前刚入门还望高手指正
Top
6 楼wvins(逸岚)回复于 2006-03-10 10:29:25 得分 0
常见的应用就是通过class of xx的变量创建xx类型的变量。
//=============================================================
也就是楼上的一段E文中的那个例子!
只不过我非常讨厌delphi在例子中动不动就把TComponent搬出来
他的例子还可以更简单点!Top




