怎样使用类引用类型,为什莫说在调用一个类或对象的方法或虚拟构造方法时很有用?(0分)

  • 主题发起人 主题发起人 zhangl1106
  • 开始时间 开始时间
Z

zhangl1106

Unregistered / Unconfirmed
GUEST, unregistred user!
如:
type
TMyClass = class of TObject;
TFun = function :integer of Object;
var
AnyObj:TMyClass;
 
请察看GetClass函数及其用途
 
没看懂
class of TObject;
好像跟TClass的定义一样。
 
老哥,没人回答,我只好烂鸟充数了(我看了这方面的几篇文章,也是一知半截)。
知道可以这么用!!
procedure TForm1.Button1Click(Sender: TObject);
type
tmyobj=class of tcontrol;
var
myobj:tmyobj;
begin
myobj:=tbutton;
myobj.Create(self);
myobj:=tcombobox;
myobj.Create(self);
end;
 
lichaohui老兄:
你能给我详细讲讲吗?举个实例行吗?
 
如果你能把这段程序运行起来,你就明白了
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, StdCtrls, Buttons, ExtCtrls, ComCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
ComboBox1: TComboBox;
Button1: TButton;
PopupMenu1: TPopupMenu;
ScrollBox1: TScrollBox;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure PopupMenuClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure ContextPopup(Sender: TObject;
MousePos: TPoint;
var Handled: Boolean);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
PopupControl: TControl;
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
RegisterClasses([TButton,TEdit,TMemo,TComboBox,TSpeedButton,
TShape,TListBox,TLabel,TCheckBox,TRadioButton,TGroupBox,
TRadioGroup,TPanel,TBitBtn,TStaticText,TRichEdit,TUpDown,
TTrackBar,TProgressBar,TTabControl,TPageControl]);
Caption := '使用右键弹出菜单删除控件';
Font.Name := '宋体';
Font.Charset := GB2312_CHARSET;
Font.Size := 9;
Label1.Caption := '请输入类名:';
ComboBox1.Items.CommaText := 'TButton,TEdit,TMemo,TComboBox' +
',TSpeedButton,TShape,TListBox';
ComboBox1.ItemIndex := 0;
Button1.Caption := '创建控件';
Button2.Caption := '全部删除';
PopupMenu1.Items.Add(NewItem(
'删除控件', 0, False, True, PopupMenuClick, 0,
'DeleteControlItem'
));
Button1.OnClick := Button1Click;
Button2.OnClick := Button2Click;
end;

procedure TForm1.PopupMenuClick(Sender: TObject);
begin
//删除弹出菜单的控件
try
if Assigned(PopupControl) and (PopupControl is TControl) then
begin
FreeAndNil(PopupControl);
end;
except
ShowMessage('对象可能已经删除');
end;
end;

type
TControlCrack = class(TControl);
procedure TForm1.Button1Click(Sender: TObject);
var
tmp: TControl;
ctrlclass: TControlClass;
tmpclass: TPersistentClass;
begin
tmpclass := GetClass(ComboBox1.Text);
if Assigned(tmpclass) then
begin
if tmpclass.InheritsFrom(TControl) then
begin
ctrlclass := TControlClass(tmpclass);
tmp := ctrlclass.Create(Self);
tmp.Left := Random(300);
tmp.Top := Random(200);
tmp.Parent := ScrollBox1;
TControlCrack(tmp).PopupMenu := PopupMenu1;
TControlCrack(tmp).OnContextPopup := ContextPopup;
end
else
ShowMessage('您输入的不是控件类,无法创建');
end
else
ShowMessage('不可识别的类名,或类未注册');
end;

procedure TForm1.ContextPopup(Sender: TObject;
MousePos: TPoint;
var Handled: Boolean);
begin
PopupControl := TControl(Sender);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
while ScrollBox1.ControlCount > 0do
ScrollBox1.Controls[0].Free;
end;

end.
 
lichaohui老兄:
感谢你的回答,我有些明白了,下面的又表示什莫意思呢?
你能给我详细讲讲吗?举个实例行吗?
type
TFun = function :integer of Object;
 
比如有一函数
function getint:integer;
begin
result:=2;
end;
则可这么用
var mygetint:TFun;
mygetint:=getint;
edit1.text:=vartostr(mygetint);
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
I
回复
0
查看
435
import
I
后退
顶部