Delphi 在不同的类中具有相同的属性和过程。如何访问它们?
我创建了几个新对象Delphi 在不同的类中具有相同的属性和过程。如何访问它们?,delphi,Delphi,我创建了几个新对象 TMyMemo = class (TMemo) private FYahoo = Integer; procedure SetYahoo(Value:integer) public procedure Google(A,B:integer; S:string); published property Yahoo:integer read FYahoo write SetYahoo; end; TMyPaintbox = class (TPaintbox) p
TMyMemo = class (TMemo)
private
FYahoo = Integer;
procedure SetYahoo(Value:integer)
public
procedure Google(A,B:integer; S:string);
published
property Yahoo:integer read FYahoo write SetYahoo;
end;
TMyPaintbox = class (TPaintbox)
private
FYahoo = Integer;
procedure SetYahoo(Value:integer)
public
procedure Google(A,B:integer; S:string);
published
property Yahoo:integer read FYahoo write SetYahoo;
end;
TMyButton = class (TButton)
private
FYahoo = Integer;
procedure SetYahoo(Value:integer)
public
procedure Google(A,B:integer; S:string);
published
property Yahoo:integer read FYahoo write SetYahoo;
end;
。
.
这些控件放在Form1上。有没有办法,我如何更改相同的属性(Yahoo)并运行过程(Google),这通常是在不同的对象中声明的
我不想手动检查类类型,如:
如果控件[i]是TMyMemo,那么。。。
如果控件[i]是TMyPaintbox,则
因为我不知道我的新类中有多少将具有属性Yahoo和方法Google(这只是一个简单的例子)。
可能我必须使用^and@operator或FieldAddress,MethodAddress,我不知道还有什么。你能帮我找到一般的解决办法吗
procedure Form1.Button1Click(Sender:TObject);
var i:integer;
begin
for i:=0 to Form1.ControlCount-1 do
begin
Controls[i].Google(4,5, 'Web'); // this should be changed somehow
Controls[i].Yahoo:=6; // this should be changed somehow
end;
end;
结束
感谢@lyborko,控件[i]返回一个没有实现
Google
方法和Yahoo
属性的TControl类。要解决您的问题,您可以使用ClassType
属性检查控件[i]
的类,然后实现类似的内容
procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
begin
for i:=0 to Form1.ControlCount-1 do
begin
if Controls[i].ClassType = TMyPaintbox then
begin
TMyPaintbox (Controls[i]).Google(4,5, 'Web');
TMyPaintbox (Controls[i]).Yahoo:=6;
end
else
if Controls[i].ClassType = TMyMemo then
begin
TMyMemo (Controls[i]).Google(4,5, 'Web');
TMyMemo (Controls[i]).Yahoo:=6;
end
else
if Controls[i].ClassType = TMyButton then
begin
TMyButton (Controls[i]).Google(4,5, 'Web');
TMyButton (Controls[i]).Yahoo:=6;
end;
end;
end;
@lyborko,控件[i]返回一个t控件类,该类没有
Google
方法和Yahoo
属性的实现。要解决此问题,可以使用ClassType
属性检查控件[i]
的类,然后实现类似的内容
procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
begin
for i:=0 to Form1.ControlCount-1 do
begin
if Controls[i].ClassType = TMyPaintbox then
begin
TMyPaintbox (Controls[i]).Google(4,5, 'Web');
TMyPaintbox (Controls[i]).Yahoo:=6;
end
else
if Controls[i].ClassType = TMyMemo then
begin
TMyMemo (Controls[i]).Google(4,5, 'Web');
TMyMemo (Controls[i]).Yahoo:=6;
end
else
if Controls[i].ClassType = TMyButton then
begin
TMyButton (Controls[i]).Google(4,5, 'Web');
TMyButton (Controls[i]).Yahoo:=6;
end;
end;
end;
定义一个同时定义了方法Google()和属性Yahoo的接口 使您的TMyButton、TMyMemo和TMyPaintbox从该接口继承并重写这些方法以执行必要的操作 在循环中,使用“as”操作符将控件强制转换为接口类型,并访问Yahoo字段和Google()方法 下面是代码-is操作符在Delphi 2009及以下版本中无法正常工作,因此我必须为此编写一个函数-它需要依赖于捕获强制转换异常,因此它不是最干净的解决方案:
type
TMyInterface = interface(IInterface)
['{1F379072-BBFE-4052-89F9-D4297B9A826F}']
function GetYahoo : Integer;
procedure PutYahoo(i : Integer);
property Yahoo : Integer read GetYahoo write PutYahoo;
procedure Google(A, B : integer; S : string);
end;
TMyButton = class (TButton, TMyInterface)
private
FStr : String;
FYah : Integer;
public
function GetYahoo : Integer;
procedure PutYahoo(i : Integer);
procedure Google(A, B : integer; S : string);
end;
TMyMemo = class (TMemo, TMyInterface)
private
FStr : String;
FYah : Integer;
public
function GetYahoo : Integer;
procedure PutYahoo(i : Integer);
procedure Google(A, B : integer; S : string);
end;
{ TMyButton }
function TMyButton.GetYahoo: Integer;
begin
Result := 0;
end;
procedure TMyButton.Google(A, B: integer; S: string);
begin
FStr := S + '=' + IntToStr(A + B);
end;
procedure TMyButton.PutYahoo(i: Integer);
begin
FYah := 42;
end;
{ TMyMemo }
function TMyMemo.GetYahoo: Integer;
begin
//
end;
procedure TMyMemo.Google(A, B: integer; S: string);
begin
//
end;
procedure TMyMemo.PutYahoo(i: Integer);
begin
//
end;
function IsMyIntf(c : TControl) : TMyInterface;
begin
try
Result := c as TMyInterface;
except on e : Exception do
Result := nil;
end;
end;
procedure TForm2.Button1Click(Sender: TObject);
var
i: Integer;
p : TMyInterface;
begin
for i := 0 to ControlCount - 1 do
begin
p := IsMyIntf(Controls[i]);
if (p <> nil) then
begin
p.PutYahoo(i);
p.Google(i, i, 'Hah!');
end;
end;
end;
类型
TMyInterface=接口(i接口)
[{1F379072-BBFE-4052-89F9-D4297B9A826F}]
函数GetYahoo:Integer;
程序(i:整数);
属性Yahoo:Integer读取GetYahoo写入PutYahoo;
过程Google(A,B:整数;S:字符串);
终止
TMyButton=类(TButton,TMyInterface)
私有的
FStr:字符串;
FYah:整数;
平民的
函数GetYahoo:Integer;
程序(i:整数);
过程Google(A,B:整数;S:字符串);
终止
TMyMemo=class(TMemo,TMyInterface)
私有的
FStr:字符串;
FYah:整数;
平民的
函数GetYahoo:Integer;
程序(i:整数);
过程Google(A,B:整数;S:字符串);
终止
{TMyButton}
函数TMyButton.GetYahoo:Integer;
开始
结果:=0;
终止
过程TMyButton.Google(A,B:integer;S:string);
开始
FStr:=S+'='+IntToStr(A+B);
终止
过程TMyButton.putyhoo(i:Integer);
开始
FYah:=42;
终止
{TMyMemo}
函数TMyMemo.GetYahoo:Integer;
开始
//
终止
过程TMyMemo.Google(A,B:integer;S:string);
开始
//
终止
过程TMyMemo.putyhoo(i:整数);
开始
//
终止
函数IsMyIntf(c:t控制):TMyInterface;
开始
尝试
结果:=c为TMY界面;
e上除外:例外
结果:=无;
终止
终止
程序TForm2.按钮1单击(发件人:ToObject);
变量
i:整数;
p:tmy接口;
开始
对于i:=0到ControlCount-1 do
开始
p:=IsMyIntf(对照[i]);
如果(p为零)那么
开始
p、 普特亚奥(一);
p、 谷歌(我,我,哈!);
终止
终止
终止
定义一个同时定义了方法Google()和属性Yahoo的接口
使您的TMyButton、TMyMemo和TMyPaintbox从该接口继承并重写这些方法以执行必要的操作
在循环中,使用“as”操作符将控件强制转换为接口类型,并访问Yahoo字段和Google()方法
下面是代码-is操作符在Delphi 2009及以下版本中无法正常工作,因此我必须为此编写一个函数-它需要依赖于捕获强制转换异常,因此它不是最干净的解决方案:
type
TMyInterface = interface(IInterface)
['{1F379072-BBFE-4052-89F9-D4297B9A826F}']
function GetYahoo : Integer;
procedure PutYahoo(i : Integer);
property Yahoo : Integer read GetYahoo write PutYahoo;
procedure Google(A, B : integer; S : string);
end;
TMyButton = class (TButton, TMyInterface)
private
FStr : String;
FYah : Integer;
public
function GetYahoo : Integer;
procedure PutYahoo(i : Integer);
procedure Google(A, B : integer; S : string);
end;
TMyMemo = class (TMemo, TMyInterface)
private
FStr : String;
FYah : Integer;
public
function GetYahoo : Integer;
procedure PutYahoo(i : Integer);
procedure Google(A, B : integer; S : string);
end;
{ TMyButton }
function TMyButton.GetYahoo: Integer;
begin
Result := 0;
end;
procedure TMyButton.Google(A, B: integer; S: string);
begin
FStr := S + '=' + IntToStr(A + B);
end;
procedure TMyButton.PutYahoo(i: Integer);
begin
FYah := 42;
end;
{ TMyMemo }
function TMyMemo.GetYahoo: Integer;
begin
//
end;
procedure TMyMemo.Google(A, B: integer; S: string);
begin
//
end;
procedure TMyMemo.PutYahoo(i: Integer);
begin
//
end;
function IsMyIntf(c : TControl) : TMyInterface;
begin
try
Result := c as TMyInterface;
except on e : Exception do
Result := nil;
end;
end;
procedure TForm2.Button1Click(Sender: TObject);
var
i: Integer;
p : TMyInterface;
begin
for i := 0 to ControlCount - 1 do
begin
p := IsMyIntf(Controls[i]);
if (p <> nil) then
begin
p.PutYahoo(i);
p.Google(i, i, 'Hah!');
end;
end;
end;
类型
TMyInterface=接口(i接口)
[{1F379072-BBFE-4052-89F9-D4297B9A826F}]
函数GetYahoo:Integer;
程序(i:整数);
属性Yahoo:Integer读取GetYahoo写入PutYahoo;
过程Google(A,B:整数;S:字符串);
终止
TMyButton=类(TButton,TMyInterface)
私有的
FStr:字符串;
FYah:整数;
平民的
函数GetYahoo:Integer;
程序(i:整数);
过程Google(A,B:整数;S:字符串);
终止
TMyMemo=class(TMemo,TMyInterface)
私有的
FStr:字符串;
FYah:整数;
平民的
函数GetYahoo:Integer;
程序(i:整数);
过程Google(A,B:整数;S:字符串);
终止
{TMyButton}
函数TMyButton.GetYahoo:Integer;
开始
结果:=0;
终止
过程TMyButton.Google(A,B:integer;S:string);
开始
FStr:=S+'='+IntToStr(A+B);
终止
过程TMyButton.putyhoo(i:Integer);
开始
FYah:=42;
终止
{TMyMemo}
函数TMyMemo.GetYahoo:Integer;
开始
//
终止
过程TMyMemo.Google(A,B:integer;S:string);
开始
//
终止
过程TMyMemo.putyhoo(i:整数);
开始
//
终止
函数IsMyIntf(c:t控制):TMyInterface;
开始
尝试
结果:=c为TMY界面;
e上除外:例外
结果:=无;
终止
终止
程序TForm2.按钮1单击(发件人:ToObject);
变量
i:整数;
p:tmy接口;
开始
对于i:=0到ControlCount-1 do
开始
p:=IsMyIntf(对照[i]);
如果(p为零)那么
开始
p、 普特亚奥(一);
p、 谷歌(我,我,哈!);
终止
终止
终止
您正在寻找RTTI。请阅读本文(参见第3页) 唯一的问题是它不能使用只发布的公共属性/方法(我不知道在Delphi2010中这是否正确)。delphi 2010的许多RTTI信息可以在这里找到<