S
smokingroom
Unregistered / Unconfirmed
GUEST, unregistred user!
别的不说,也说说"接口",次时代网友的理解有些误差,例子也是错误的.
接口可以理解为定义行为的类,它本身并不能实现功能,依靠派生类实现.
下面是我写着玩的一段代码,有接口声明,其实现接口的派生类的声明,可以参考一下
type
TGraphicType=(gtRectangle, gtCircle, gtLine);
IGraphic=interface(IInterface)
['{A1D2DED6-523D-45E9-A1EF-0F91B67C6FBE}']
procedure Draw(ACanvas:TCanvas);
function GetColor:TColor;
procedure SetColor(Value:TColor);
property Color:TColor read GetColor write SetColor;
end;
TCircle=class(TInterfacedObject, IGraphic)
private
FCenter:TPoint;
FRadius:Integer;
FColor: TColor;
function GetColor:TColor;
procedure SetColor(Value:TColor);
public
constructor Create(ACenter:TPoint;
ARadius:Integer);
destructor Destroy;override;
procedure Draw(ACanvas:TCanvas);
property Center:TPoint read FCenter write FCenter;
property Radius:Integer read FRadius write FRadius;
property Color:TColor read GetColor write SetColor;
end;
TRectangle=class(TInterfacedObject,IGraphic)
private
FLefttop:TPoint;
FRightbottom:TPoint;
FColor:TColor;
function GetColor:TColor;
procedure SetColor(Value:TColor);
public
constructor Create(ALefttop,ARightbottom:TPoint);
destructor Destroy;override;
procedure Draw(ACanvas:TCanvas);
property Lefttop:TPoint read FLefttop write FLefttop;
property Rightbottom:TPoint read FRightbottom write FRightbottom;
property Color:TColor read GetColor write SetColor;
end;
TLine=class(TInterfacedObject,IGraphic)
private
FStartPoint:TPoint;
FEndPoint:TPoint;
FColor:TColor;
function GetColor:TColor;
procedure SetColor(Value:TColor);
public
constructor Create(AStartPoint,AEndPoint:TPoint);
destructor Destroy;override;
procedure Draw(ACanvas:TCanvas);
property StartPoint:TPoint read FStartPoint write FStartPoint;
property EndPoint:TPoint read FEndPoint write FEndPoint;
property Color:TColor read GetColor write SetColor;
end;
接口可以理解为定义行为的类,它本身并不能实现功能,依靠派生类实现.
下面是我写着玩的一段代码,有接口声明,其实现接口的派生类的声明,可以参考一下
type
TGraphicType=(gtRectangle, gtCircle, gtLine);
IGraphic=interface(IInterface)
['{A1D2DED6-523D-45E9-A1EF-0F91B67C6FBE}']
procedure Draw(ACanvas:TCanvas);
function GetColor:TColor;
procedure SetColor(Value:TColor);
property Color:TColor read GetColor write SetColor;
end;
TCircle=class(TInterfacedObject, IGraphic)
private
FCenter:TPoint;
FRadius:Integer;
FColor: TColor;
function GetColor:TColor;
procedure SetColor(Value:TColor);
public
constructor Create(ACenter:TPoint;
ARadius:Integer);
destructor Destroy;override;
procedure Draw(ACanvas:TCanvas);
property Center:TPoint read FCenter write FCenter;
property Radius:Integer read FRadius write FRadius;
property Color:TColor read GetColor write SetColor;
end;
TRectangle=class(TInterfacedObject,IGraphic)
private
FLefttop:TPoint;
FRightbottom:TPoint;
FColor:TColor;
function GetColor:TColor;
procedure SetColor(Value:TColor);
public
constructor Create(ALefttop,ARightbottom:TPoint);
destructor Destroy;override;
procedure Draw(ACanvas:TCanvas);
property Lefttop:TPoint read FLefttop write FLefttop;
property Rightbottom:TPoint read FRightbottom write FRightbottom;
property Color:TColor read GetColor write SetColor;
end;
TLine=class(TInterfacedObject,IGraphic)
private
FStartPoint:TPoint;
FEndPoint:TPoint;
FColor:TColor;
function GetColor:TColor;
procedure SetColor(Value:TColor);
public
constructor Create(AStartPoint,AEndPoint:TPoint);
destructor Destroy;override;
procedure Draw(ACanvas:TCanvas);
property StartPoint:TPoint read FStartPoint write FStartPoint;
property EndPoint:TPoint read FEndPoint write FEndPoint;
property Color:TColor read GetColor write SetColor;
end;