两个类:
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 属性 
    

解决方案 »

  1.   

    typeTBase = class
      int : Integer;
    end;TClassA = class(TBase)
    end;TClassB = class(TBase)
    end;procedure AAA(aClass : TBase);
      

  2.   

    ---------------------------------
    比如:  procedure AAA(aClass : TClass );
    aClass 可以是任意一个类(TMyclass1 或是 TMyclass2)
    接着 aClass.int := XXX  我要的就是这个!!!
    ----------------------------------你的思路有问题,还是先去搞清楚对象、泛型、多态这些基本概念吧
      

  3.   

    这些语法上的东东都可以先不要管关键是如何可以做到:总结:就是 "TMyclass1(XXX).int"  "TMyclass2(XXX).int"  怎样可以整合到一起,只用一种代码
         问题就在 如果不 "TMyclass1(XXX)." 程序就认不到 int 属性
      

  4.   

    让TMyClass1和TMyClass2继承自同一个父类TBase
    给你段代码,自己体会吧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.
      

  5.   

    多谢 sdzeng(大头鸟) 的帮忙可是:
    -----------------------------
        c1 := TClassA.Create;
        c1.int := 100;           //这里这句已经事先写好了
        c2 := TClassB.Create;
        c2.int := 200;           //这里这句已经事先写好了 
    -----------------------------我要的是不要事先写好 c1.int 或者 c2.int  让程序在运行时自己赋值
    我就是不知道如果不 c1 := TClassA.Create;  
    能不能让程序自己识别 c1.int 
      

  6.   

    你的思路是过程的参数可以在运行动态决定是哪一个具体类的对象,是这样吗?rrhrrhf(过路的) 给出的方法在一定范围内可行,也就是说的在有亲族关系的类的情况下,通过向上转型来实现。但你的要求好象不止如此,你说的是对任意类都可以用“procedure AAA(aClass : TClass )”,不一定非得有血亲关系。解决方法:首先要划定一个范围,不可能这个过程能包罗万象,并不是所有的类都有int这个数据字段(属性与字段是有区别的),其次,将过程的参数不定义成某一类,而是定义为接口,如:“procedure AAA(aInterfce : TsomeInterface )”,你要处理的类只要实现了此接口TsomeInterface并且在此接口中包含有int这个属性(注意:此处叫属性),就能达到你的要求。关于接口,有一定难度,请查相关资料。
      

  7.   

    to fjwolf():
    ---------------------------------------------------------
    并不是所有的类都有int这个数据字段(属性与字段是有区别的)
    ---------------------------------------------------------对啊,就是因为这个
    你说的这个接口,我就不大懂了
      

  8.   

    这个问题的由来:
    有N个窗体,每个窗体 public 中都有个 str : string
    现在我要一方法
    procedure AAA(窗体名:TForm) 
    begin
      窗体名.str := 'abc';   //类似这样的,给任意一个窗体的 str 赋值
    end;可是,如果不事先类型转换 TForm1(窗体名) 就调不出 str 了
      

  9.   

    看看这个对你有没有帮助:
    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.
      

  10.   

    to rrhrrhf(过路的):在接口中的属性,实际上就是存和取的过程与函数,具体由继承这个接口的类来实现。如果一接口中有int 这个属性,存过程、取函数由你来写,你可以有较大的发挥空间。
      

  11.   

    多谢大家的帮忙!
    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 并赋值
       
      

  12.   

    代码功能基本上是一样的!>>>>>不过我的本意是想能不能让程序自己找出某某类里有些什么方法,属性
    可以加入不同的接口比如:
      TClass1= class(TComponent, IBase, IBaseInt, IBaseFloat);  TClass2 = class(TComponent, IBase, IBaseDatetime, IBaseChar);然后可以使用QueryInterface来查看某个对象是否支持某个接口,
    如果支持则可以进行强制转换,并调用该接口的方法
      

  13.   

    unit Unit1;interfaceuses
      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.
      

  14.   

    要调用到STR,做一个基类,包含这个STR,其他的类从这个地方继承就行了
    不就都包含了吗
      

  15.   


      to zwjchina(蒲石):
       你可以说说 queryinterface 的用法吗?
       CSDN 搜索正在维护... 不能用了
       
      to merkey2002(小样的): 多谢你的代码
      
     
      to notruiyi(notruiyi):  ????
      

  16.   

    unit Unit1;interfaceuses
      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.
      

  17.   

    楼主还是思路不太对啊
    先学点面向对象的知识吧
    sdzeng(大头鸟) 的思路就可以解决你的问题了
      

  18.   

    非常感谢  zwjchina(蒲石)  
    问题解决了
    结贴了