你的概念错了,参数只能是一个实例或指针,但你试途把一个类型做为参数。而且FFrame是在哪定义的?
解决方案 »
- Delphi XE2 下使用 SQLStoredProc1控件 获取不到存储过程返回的参数数据
- 向listview1指定的位置写入字符串?
- 哪位能提供一份软件设计文档?
- DBGrid什么都不显示了!
- 输出问题。。急。。。。
- 请问应该如果解开winzip 压缩的文件?
- fastreport能打印主从表???怎么实现?
- 把listview的viewstyle属性设为vsReport。怎样使得popupmenu只在鼠标右击listview中的item时弹出,而右击listview的其他地方时不弹出?怎样
- 如何解决Delphi中FastReport打印图片时分辨率低的问题?
- AD数据选择问题?
- 不好意思,问一个比较菜的问题
- 关于dbgrid的问题请教大虾。
type TClass = Class of TObject;
?处填写TClass;
可以给分了,呵呵
A class-reference type, sometimes called a metaclass, is denoted by a construction of the formclass of typewhere type is any class type. The identifier type itself denotes a value whose type is class of type. If type
1 is an ancestor of type2, then class of type2 is assignment-compatible with class of type1. Thustype 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.
你的方法我在提问以前,就式过了,不行FFrame在函数外已经定义:
var
FFrame:TFrame;
在TObject类中没法找到这个构造函数,所以会出错。你应该把定义改成:
type TComponetClass = class of TComponent;
ok!
type TFrameClass = class of TFrame;
才对,因为不能将TComponent类赋值给它的子类的变量。
原程序是:
procedure TForm1.ButtonClick(Sender:Tobject);
begin
if FFrame<>nil then
FFrame.free;
FFrame := Tlei.Create(Panel1);
FFrame.Parent := Panel1;
end;
但Frame不止一个所以我想写成一个过程,希望各位能说详细一点,你们说的我都不大明白
在使用时,class of TObject 可以表示任何类,class of TFrame则只能表示TFrame类或其子类,将TFrame类的一个子类赋值给这个变量后,调用构造函数,构造出的类是子类的类型。也就达到了传递类型的作用。
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;type
TClass=Class of TFrame;
TObjClass=Class of TObject;
TForm1 = class(TForm)
Button1: TButton;
Panel1: TPanel;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
FFrame:TFrame;
procedure CreateObj(ObjectCls:TClass);
procedure CreateObjEx(ObjectCls:TObjClass);
public
{ Public declarations }
end;var
Form1: TForm1;implementationuses Unit2, Unit3, Unit4;{$R *.DFM}
procedure TForm1.CreateObj(ObjectCls:TClass);
begin
if Assigned(FFrame) then
begin
FFrame.Free();
FFrame:=nil;
End;
FFrame:=ObjectCls.Create(Self);
FFrame.Parent:=Panel1;
end;procedure TForm1.CreateObjEx(ObjectCls:TObjClass);
Var
FrameClass:TClass;
Begin
if Assigned(FFrame) then
begin
FFrame.Free();
FFrame:=nil;
End;
FrameClass:=TClass(ObjectCls);
FFrame:=FrameClass.Create(Self);
FFrame.Parent:=Panel1;
End;procedure TForm1.Button1Click(Sender: TObject);
begin
CreateObj(TFrame2);
end;procedure TForm1.FormCreate(Sender: TObject);
begin
FFrame:=Nil;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
CreateObj(TFrame3);
end;procedure TForm1.Button3Click(Sender: TObject);
begin
CreateObjEx(TFrame4);end;End.
procedure TForm1.CreateObj(ObjectCls:TClass);
begin
if Not Assigned(FFrame) then
begin
FFrame:=ObjectCls.Create(Self);
FFrame.Parent:=Panel1;
End;
end;
你这么写虽然没有错,但我想,一般要怎么动态创建TFrame一般是为了实现在各Frame之间切换,你这么写,就实现不了该功能了!当然程序中的“FFrame:=nil;”是多余的,只是写程序习惯了,Free后紧接着设为nil,(在delphi5以后还可以用FreeAndNil函数)。