Delphi Prism:这是深复制还是浅复制?
看一下下面的代码:
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;
我认为这就是对对象进行深度复制克隆的方式。如果这是真的,那么这些对象应该表现得好像它们是单独的对象一样,但事实并非如此。例如,每当我改变笔的宽度时,它也会改变原始对象的笔的宽度。请帮忙。
提前致谢。
Take a look at the following code:
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;
I am thinking that this is how you would do deepcopy cloning of an object. If that is true, these objects should act as if they are separate object, but it doesn't. Any time I change, for instance, thepen's width it also changes the thepen width of the original object. Please, help.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个浅克隆;
thePen
不是克隆的,而是在两个实例之间共享的。而不是
你应该克隆每个单独的属性,像这样(注意:伪代码)
This is a shallow clone;
thePen
is not cloned but shared between the two instances.Instead of
you should clone each individual property, something like this (note: pseudocode)