2011-12-30 4 views
0

는 다음 코드를 살펴 :이 당신이 객체의 deepcopy 복제를 할 것 어떻게 생각하고Delphi Prism :이 deepcopy 또는 shallowcopy입니까?

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; 

. 그것이 사실이라면,이 객체들은 마치 별도의 객체처럼 행동해야하지만, 그렇지 않습니다. 예를 들어 테 펜의 너비를 변경할 때마다 원본 개체의 테 이펜 너비도 변경됩니다. 도와주세요.

미리 감사드립니다.

답변

1

이것은 얕은 복제물입니다. thePen은 복제되지 않지만 두 인스턴스간에 공유됩니다. (: 의사 주)

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

romkyns :

가 대신

newRect.Assign(Self); 

의이 같은 각각의 특성, 뭔가 복제해야 우수함, 그것을 작동합니다. 고맙습니다. – ThN

관련 문제