【问题标题】:Delphi Prism : Is this deepcopy or shallowcopy?Delphi Prism:这是深拷贝还是浅拷贝?
【发布时间】:2012-01-30 18:20:11
【问题描述】:

看看下面的代码:

method TMakerRect.Clone: TMakerObject;
var
  newRect:TMakerRect;
begin
  newRect := new TMakerRect(bounds,myForm);
  newRect.Assign(Self);
  newRect.theBrushStyle := Self.theBrushStyle;
  Result := newRect;
end;

method TMakerGraphic.Assign(obj:TMakerObject);
begin
  inherited Assign(obj);
  if obj is TMakerGraphic then
  begin
       thePen:=TmakerGraphic(obj).thePen;
       FillColor:=TMakerGraphic(obj).fillColor;
       dynamics:=TmakerGraphic(obj).dynamics;
  end;
end;

我认为这就是您对对象进行深拷贝克隆的方式。如果这是真的,这些对象应该表现得好像它们是单独的对象,但事实并非如此。例如,每当我改变笔的宽度时,它也会改变原始对象的笔宽度。请帮忙。

提前致谢。

【问题讨论】:

    标签: .net object clone delphi-prism


    【解决方案1】:

    这是一个浅克隆; thePen 没有被克隆,而是在两个实例之间共享。

    代替

    newRect.Assign(Self);
    

    你应该克隆每个单独的属性,像这样(注意:伪代码)

    newRect.thePen := self.thePen.Clone();
    etc...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-09
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-19
      相关资源
      最近更新 更多