可以使用assign来复制对象的对象吗? [英] Can I use assign to duplicate an object of objects?

查看:172
本文介绍了可以使用assign来复制对象的对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从TPersistent继承于3度的对象,我想使用分配过程。

  MyFirstObj:= GrandSonOfPersistent.Create() ; 
//我修改MyFirstObj中的对象
MySecondObj:= GrandSonOfPersistent.Create();
MySecondObj.Assign(MyFirstObject);

如何检查它是否有效?
当对象有很多其他对象时,它是否工作?



我正在尝试克隆一个对象,这是正确的方法吗?

解决方案

分配是一种虚拟方法。任何从 TPersistent 继承的后代类应该覆盖 Assign 来处理在基础之上添加的任何新成员的深度复制类。如果您的课程不覆盖分配来处理这些深层副本,则使用分配使此副本不会成功。调用分配的基本实现调用 AssignTo ,尝试使用源对象的实现来执行副本。如果源和目标对象都不能处理该副本,则会出现异常。



请参阅:文档



例如:

  unit SomeUnit; 

界面

使用类;

type
TMyPersistent = class(TPersistent)
private
FField:string;
public
属性字段:字符串读取FField写入FField;
程序分配(APersistent:TPersistent);覆盖
结束

实现

过程TMyPersistent.Assign(APersistent:TPersistent);
begin
如果APersistent是TMyPersistent,那么
Field:= TMyPersistent(APersistent).Field
else
inherited Assign(APersistent);
结束

结束。请注意,从 TPersistent 继承的任何类应该应该被应用于只能调用继承,如果它不能处理分配调用。然而,后代类应该始终调用继承,因为父代也可能有动作来执行,如果没有,将处理传递调用基础继承

  type 
TMyOtherPersistent = class(TMyPersistent)
private
FField2:string;
public
属性Field2:字符串读取FField2写入FField2;
程序分配(APersistent:TPersistent);覆盖
结束

实现

过程TMyPersistent.Assign(APersistent:TPersistent);
begin
如果APersistent是TMyOtherPersistent,那么
Field2:= TMyOtherPersistent(APersistent).Field2;
继承Assign(APersistent);
结束

在这个例子中,我显示了字符串。对于对象成员,您需要使用 Assign 方法或以其他方式执行副本。


I have an object that inherits in 3rd degree from TPersistent and I want to clone it using the Assign procedure.

MyFirstObj := GrandSonOfPersistent.Create();
//I modify the objects inside MyFirstObj
MySecondObj := GrandSonOfPersistent.Create();
MySecondObj.Assign(MyFirstObject);

How can I check it worked? Does it work when the objects have many other objects?

I am trying to clone an object, is this the correct way to do it?

解决方案

Assign is a virtual method. Any descendent classes that inherit from TPersistent should override Assign to handle deep copying of any new members added on top of the base class. If your classes do not override Assign to handle these deep copies then using Assign to make such a copy will not be successful. The base implementation of Assign calls AssignTo which attempts to use the source object's implementation to perform the copy. If neither source nor destination object can handle the copy then an exception is raised.

See : The Documentation

For Example:

unit SomeUnit;

 interface

 uses Classes;

 type
   TMyPersistent = class(TPersistent)
   private
     FField: string;         
   public
     property Field: string read FField write FField;
     procedure Assign (APersistent: TPersistent) ; override;
   end;

 implementation

 procedure TMyPersistent.Assign(APersistent: TPersistent) ;
 begin        
    if APersistent is TMyPersistent then
      Field := TMyPersistent(APersistent).Field          
    else         
      inherited Assign (APersistent);
 end;

end. 

Note that any class inheriting from TPersistent should only call inherited if it cannot handle the Assign call. A descendent class, however, should always call inherited since the parent may also have actions to perform and, if not, will handle passing calling the base inherited :

type
  TMyOtherPersistent = class(TMyPersistent)
  private
    FField2: string;         
  public
    property Field2: string read FField2 write FField2;
    procedure Assign (APersistent: TPersistent) ; override;
  end;

implementation

procedure TMyPersistent.Assign(APersistent: TPersistent) ;
begin 
  if APersistent is TMyOtherPersistent then
    Field2 := TMyOtherPersistent(APersistent).Field2;     
  inherited Assign (APersistent);  
end;

In this example I've shown strings. For object members you would either need to use their Assign methods or perform the copy in some other way.

这篇关于可以使用assign来复制对象的对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆