是这样的,我有一个Tfriend的对象,也就是程序中好友的对象(一个类IM软件)。然后我想在程序运行中每添加一个好友就新建一个实例,但不知道怎么实现,求解?

解决方案 »

  1.   


    .....
    TFriendClass = class of TFriend;....var
       clazz : TFriendClass;
    begin
         clazz := TFriend;
         clazz.newInstance;
       ....
    end;
    newInstance will only call default constructor. i.e. constructor without any arguments.Hope it helps.//Ali
      

  2.   


    关于上面的代码我有几点疑问,谢谢
    1.为什么一定要用类类型?
    为什么不能这样?
    var
       clazz : TFriend;
    begin
         clazz := TFriend.create;
     
       ....
    end;2.我想让每个生成的实例动态命名,这怎么实现?
    是否是这样?
    clazz.name:=xxxxxx
      

  3.   


    If you have only one class then for sure you can use that in this way, but in case where you need to invoke different instances based on dynamic parameters then class of is your best friend.
    Confused!!!! What are you trying to achieve? You got conflict in your statement 1 and 2. From statement 2 look like you want to have invoke different class instances.Are you talking about naming variables?//Ali
      

  4.   

    你定义一个全局动态数组:
    var
    FFriends :array of TAFriend ;每添加一个好友就新建一个实例 ;
    var
       f :TAFriend ;begin
          SetLength(FFriends , Length(FFriends ) + 1);
          f := TAFriend.create() ;
          f.Name := ...//设置属性
           FFriends[High(FFriends)] := f ;
      

  5.   

    遍历:
    var 
       i: integer; 
       f :TAFriend ;
    begin
       
       for (i := 0 to High(FFriends)) do 
       begin
          f := FFriends[i] ;
       end; 
    end;
      

  6.   

    1.关于为什么用类类型,这样使用灵活性高,如TFORMCLASS,可以创建所有从TFORM继承的窗体,可以自己理解一下。
    2.你要给对象明名,可能是为了区分对象,可以在对象里面加个属性就可以解决。
    3.建议,可以建一个TFriends = tobjectlist来管理TFriend对象。