2012-07-11 11 views
1

에서 상속하는 클래스의 itemClass 속을 변경하는 방법 ("TMyCollection"를 호출 할 수 있습니다) 나는 그것에서 새로운 클래스를 상속해야내가 된 TCollection에서 상속하는 클래스가 된 TCollection

("TMyItems"를 호출 할 수 있습니다)

일반적으로 우리는 TCollection의 생성자에서 ItemClass 유형을 전달하지만, 제 경우에는 TMyCollection의 생성자가 ItemClass를 사용하지 않고 Owner 만 사용하는 새 생성자로 대체됩니다.

상속 된 생성자가 ItemClass 매개 변수를 허용하지 않으면 "TMyItems"에서 ItemClass를 변경하는 방법을 알아야합니다.

감사합니다.

답변

1

당신은 여전히 ​​동일한 서명이없는 경우에도, 서브 클래스 내에서 상속 TCollection.Create를 호출 할 수 있습니다 : 원래 포스터의 의견에 따라 경찰

:

TMyCollectionItem = class(TCollectionItem) 
    private 
    FIntProp: Integer; 
    procedure SetIntProp(const Value: Integer); 
    public 
    property IntProp: Integer read FIntProp write SetIntProp; 
    end; 

    TMyCollection = class(TCollection) 
    public 
    constructor Create(AOwner: TComponent);virtual; 
    end; 

{ TMyCollection } 

constructor TMyCollection.Create(AOwner: TComponent); 
begin 
    inherited Create(TMyCollectionItem); // call inherited constructor 
end; 

편집을 , "트릭"은 새 생성자를 오버로드로 표시하는 것입니다. 오버로드이므로 TCollection 생성자에 대한 액세스를 숨기지 않습니다.

TMyCollection = class(TCollection) 
    public 
    constructor Create(AOwner: TComponent);overload; virtual; 
    end; 

    TMyItem = class(TCollectionItem) 
    private 
    FInt: Integer; 
    public 
    property Int: Integer read FInt; 
    end; 

    TMyItems = class(TMyCollection) 
    public 
    constructor Create(AOwner: TComponent);override; 
    end; 

implementation 

{ TMyCollection } 

constructor TMyCollection.Create(AOwner: TComponent); 
begin 
    inherited Create(TCollectionItem); 

end; 

{ TMyItems } 

constructor TMyItems.Create(AOwner: TComponent); 
begin 
    inherited Create(TMyItem); 
    inherited Create(AOwner); // odd, but valid 
end; 

end. 
+0

문제는 내가 상속 의 2 개 수준을 가지고있는 코드는 다음과 같은 : TMyCollection = 클래스 (된 TCollection) .... TMyCollection의 생성자가 생성자 (AOwner 만들기입니다 : TComponent); TMyItems = 클래스 (TMyCollection) TMyItems의 생성자 : 생성자 Create (AOwner : TComponent); TMyItems에서 상속 된 생성자를 호출하려고하면 ItemClass가 새로 생성되어 컬렉션에 전달되어야하므로 원하는 항목이 아닌 TMyCollection의 생성자를 호출합니다 ... – user1512094

+0

New ItemClass도 TMyCollectionItem에서 상속됩니다. TMyCollectionItem의 모든 속성과 이벤트를 갖습니다. – user1512094