两个类:
Type
TMyclass1 = class
int : integer;
end;Type
TMyclass2 = class
int : integer;
end;我想实现一个方法:
可以任意的给 TMyclass1 或 TMyclass2 的 int 赋值 比如: procedure AAA(aClass : TClass );
aClass 可以是任意一个类(TMyclass1 或是 TMyclass2)
接着 aClass.int := XXX 我要的就是这个!!!
可是在编写程序的时候我们输入 "aClass." 系统认不到 int 这个属性
可是我们又不能直接指定 TMyclass1(XXX).int 因为我们要的是任意类都可以总结:就是 "TMyclass1(XXX).int" "TMyclass2(XXX).int" 怎样可以整合到一起,只用一种代码
问题就在 如果不 "TMyclass1(XXX)." 程序就认不到 int 属性
Type
TMyclass1 = class
int : integer;
end;Type
TMyclass2 = class
int : integer;
end;我想实现一个方法:
可以任意的给 TMyclass1 或 TMyclass2 的 int 赋值 比如: procedure AAA(aClass : TClass );
aClass 可以是任意一个类(TMyclass1 或是 TMyclass2)
接着 aClass.int := XXX 我要的就是这个!!!
可是在编写程序的时候我们输入 "aClass." 系统认不到 int 这个属性
可是我们又不能直接指定 TMyclass1(XXX).int 因为我们要的是任意类都可以总结:就是 "TMyclass1(XXX).int" "TMyclass2(XXX).int" 怎样可以整合到一起,只用一种代码
问题就在 如果不 "TMyclass1(XXX)." 程序就认不到 int 属性
int : Integer;
end;TClassA = class(TBase)
end;TClassB = class(TBase)
end;procedure AAA(aClass : TBase);
比如: procedure AAA(aClass : TClass );
aClass 可以是任意一个类(TMyclass1 或是 TMyclass2)
接着 aClass.int := XXX 我要的就是这个!!!
----------------------------------你的思路有问题,还是先去搞清楚对象、泛型、多态这些基本概念吧
问题就在 如果不 "TMyclass1(XXX)." 程序就认不到 int 属性
给你段代码,自己体会吧unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TBase = class
int : Integer;
end; TClassA = class(TBase)
end; TClassB = class(TBase)
end; TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; procedure aaa(aClass : TBase) ;var
Form1: TForm1;implementation{$R *.dfm}
procedure aaa(aClass : TBase) ;
begin
ShowMessage(IntToStr(aClass.int));
end;procedure TForm1.Button1Click(Sender: TObject);
var
c1 : TClassA;
c2 : TClassB;
c : TBase;
begin
c1 := TClassA.Create;
c1.int := 100;
c2 := TClassB.Create;
c2.int := 200; c := c1;
aaa(c1);
aaa(c); c := c2;
aaa(c2);
aaa(c); c1.Free;
c2.Free;
end;end.
-----------------------------
c1 := TClassA.Create;
c1.int := 100; //这里这句已经事先写好了
c2 := TClassB.Create;
c2.int := 200; //这里这句已经事先写好了
-----------------------------我要的是不要事先写好 c1.int 或者 c2.int 让程序在运行时自己赋值
我就是不知道如果不 c1 := TClassA.Create;
能不能让程序自己识别 c1.int
---------------------------------------------------------
并不是所有的类都有int这个数据字段(属性与字段是有区别的)
---------------------------------------------------------对啊,就是因为这个
你说的这个接口,我就不大懂了
有N个窗体,每个窗体 public 中都有个 str : string
现在我要一方法
procedure AAA(窗体名:TForm)
begin
窗体名.str := 'abc'; //类似这样的,给任意一个窗体的 str 赋值
end;可是,如果不事先类型转换 TForm1(窗体名) 就调不出 str 了
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; IEditInt = interface
function EditInt(Value: Integer): Integer;
end; TClassA = class(TComponent, IEditInt)
private
i: Integer;
public
function EditInt(Value: Integer): Integer;
end; TClassB = class(TComponent, IEditInt)
private
i: Integer;
public
function EditInt(Value: Integer): Integer;
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
a: TClassA;
b: TClassB;
ref: IEditInt;
begin
a := TClassA.Create(self);
b := TClassB.Create(Self); ref := a;
ref.EditInt(100); ref := b;
ref.EditInt(200);
end;{ TClassB }function TClassB.EditInt(Value: Integer): Integer;
begin
i := Value;
end;{ TClassA }function TClassA.EditInt(Value: Integer): Integer;
begin
i := Value;
end;end.
to zwjchina(蒲石) : 谢谢你的代码,又让我学到了 ^-^看看下面的代码是不是跟您给出的代码功能一样?Type TPeople = class
procedure Who; virtual; abstract;
end; TMan = class(TPeople)
public
procedure Who; override;
end; TWoman = class(TPeople)
public
procedure Who; override;
end;...{ TMan}procedure TMan.Who;
begin
inherited;
ShowMessage('I 是 Man');
end;{ TWoman}procedure TWoman.Who;
begin
inherited;
ShowMessage('I 是 Woman ');
end;procedure TForm2.btn1Click(Sender: TObject);
var Man: TMan;
Woman: TWoman;
People: TPeople;
begin
Man := TMan.Create;
Woman := TWoman.Create; People := Man;
People.Who; People := Woman;
People.Who;end;
用接口的方法已经可以了,不过我的本意是想能不能让程序自己
找出某某类里有些什么方法,属性
就像:
type
TMyclass = class
public
i : integer;
s : string;
r : real;
end;让程序自己找出 TMyclass 类里有 i,s,r 并赋值
可以加入不同的接口比如:
TClass1= class(TComponent, IBase, IBaseInt, IBaseFloat); TClass2 = class(TComponent, IBase, IBaseDatetime, IBaseChar);然后可以使用QueryInterface来查看某个对象是否支持某个接口,
如果支持则可以进行强制转换,并调用该接口的方法
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,TypInfo;type
TMyclass1 = class(TPersistent)
private
Fhaha:integer;
published
property haha:integer read Fhaha Write Fhaha;
end; TMyclass2 = class(TPersistent)
private
Fhaha:integer;
published
property haha:integer read Fhaha Write Fhaha;
end; TForm1 = class(TForm)
Button2: TButton;
procedure Button2Click(Sender: TObject);
{ Private declarations }
public
{ Public declarations }
end;procedure AAA(Obj : TObject );var
Form1: TForm1;implementation
{$R *.dfm}procedure AAA(Obj : TObject );
begin
if not(Obj is TPersistent) then exit; //必须继承TPersistent
if GetPropInfo(Obj,'haha')<>nil then //对象中有这个属性
begin
if PropType(Obj, 'haha')=tkInteger then //这个属性是int型,字符型是tkLString(为什么不是tkString,我也搞不懂)
SetOrdProp(Obj,'haha',5); //给这个属性赋值
end;
end;procedure TForm1.Button2Click(Sender: TObject);
var
Obj1:TMyclass1;
Obj2:TMyclass2;
begin
Obj1:=TMyclass1.Create ;
AAA(Obj1);
showmessage(inttostr(obj1.haha));
Obj1.Free;
Obj2:=TMyclass2.Create ;
AAA(Obj2);
showmessage(inttostr(obj2.haha));
Obj2.free;
end;end.
不就都包含了吗
to zwjchina(蒲石):
你可以说说 queryinterface 的用法吗?
CSDN 搜索正在维护... 不能用了
to merkey2002(小样的): 多谢你的代码
to notruiyi(notruiyi): ????
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;const
GUIDIBASEINT: TGUID = '{DA3D9D23-DD8B-46E9-8B17-26A1FFA57E1D}';type
IBase = interface
['{9BA3488D-C71A-4D70-9F9A-BAEA56E672B0}']
end; IBaseInt = interface(IBase)
['{DA3D9D23-DD8B-46E9-8B17-26A1FFA57E1D}']
procedure ShowMsg;
end; TForm1 = class(TForm, IBase, IBaseInt)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure ShowMsg;
end;
var
Form1: TForm1;implementation{$R *.dfm}{ TForm1 }procedure TForm1.ShowMsg;
begin
ShowMessage('OK');
end;procedure TForm1.Button1Click(Sender: TObject);
var
lIBase: IBase;
lIBaseInt: IBaseInt;
begin
lIBase := Self;
if lIBase.QueryInterface(GUIDIBASEINT, lIBaseInt) = S_OK then
begin
lIBaseInt.ShowMsg;
end;
end;end.
先学点面向对象的知识吧
sdzeng(大头鸟) 的思路就可以解决你的问题了
问题解决了
结贴了