end; TBaseShape = class(TNoRefCount, IShape) private fPosition: TPoint; function GetPosition: TPoint; procedure SetPosition(Value: TPoint); public constructor Create; virtual; procedure Draw(Canvas: TCanvas); virtual; abstract; property Position: TPoint read fPosition write SetPosition; end; TPolygon = class(TBaseShape, IPolygon) private fVertices: array of TPoint; public procedure Draw(Canvas: TCanvas); override; function NumVertices: Integer; function NumSides: Integer; function SideLength(Index: Integer): Integer; function Vertex(Index: Integer): TPoint; end; TRectangle = class(TBaseShape, IPolygon, IRectangle) private fRect: TRect; public procedure Draw(Canvas: TCanvas); override; function NumVertices: Integer; function NumSides: Integer; function SideLength(Index: Integer): Integer; function Vertex(Index: Integer): TPoint; end; TSquare = class(TBaseShape, IPolygon, IRectangle, ISquare) private fSide: Integer; public procedure Draw(Canvas: TCanvas); override; function Side: Integer; function NumVertices: Integer; function NumSides: Integer; function SideLength(Index: Integer): Integer; function Vertex(Index: Integer): TPoint; end;
派生类继承了祖先类实现的接口。TRectangle继承自TBaseShape,则TBaseShape实现了IShape接口也就是TRectangle实现了IShape接口。而接口的继承与此有些不同。接口的继承仅仅为了类型上的便利,也就是说你不必重新再去输入许多方法的声明。当一个类实现一个接口时,并不意味着该类自动的实现了祖先的接口。事实上,该类只实现了出现在该类的声明部分的这些接口(以及在祖先类的声明部分出现的接口)。因此,即使IRectangle继承自IPolygon,TRectangle类还是得将IRectangle和IPolygon显式的罗列出来。
要实现类型体系,你不应当使用引用计数。相反,你需要实现显式的内存管理,如同处理普通的Delphi对象一样。在这种情况下,实现_AddRef和_Release 方法的最好办法就是连根拔除,就象我们在例 2-13里见到的TNoRefCount类那样。还有需要注意的是,不要有任何变量指向失效的引用。一个已经被释放的对象引用可能导致问题,因为Delphi将会自动调用_Release方法。也就是说,永远不要尝试使用指向无效指针的变量,使用接口而不使用引用计数强制你必须这么做。
PartI
PartII
PartIII
PartIV
PartV
PartVI
更多文章
上一页 [1] [2] [VB.NET程序]VB.Net中文教程(13) Whole-Part关系 [Delphi程序]The Delphi Object Model (PART III) [Delphi程序]The Delphi Object Model (PART II) [Delphi程序]The Delphi Object Model (PART I) [Delphi程序]Delphi对象模型(Part III) [Delphi程序]Delphi对象模型(Part II) [Delphi程序]Delphi对象模型(Part I) [Delphi程序]Delphi对象模型(Part V) [Delphi程序]Delphi对象模型(Part IV) [Delphi程序]防止全局hook入侵Delphi版,2000以上系统适用(pa…
|