一个MM的图,演示类及类包含的东东.

原贴见:http://community.csdn.net/Expert/topic/3793/3793756.xml
这里的是他的MM图,有MM的可以导进去看看。



代码 pro3.pas
unit pro3;

interface

uses
 SysUtils,Dialogs,Contnrs;

type
  TProduct = class(TObject)
  private
    FName: string;
    FPrice: Double;
    function GetName: string;
    function GetPrice: Double;
    procedure SetName(const Value: string);
    procedure SetPrice(Value: Double);
  public
    constructor Create; overload;
    procedure SayHello; virtual;
    property Name: string read GetName write SetName;
    property Price: Double read GetPrice write SetPrice;
  end;
 
  TProductXY = class(TProduct)
  public
    procedure SayHello; overload;
  end;
 
  TProductZY = class(TProduct)
  public
    procedure SayHello; overload;
  end;
 
  TStore = class(TObject)
  private
    FProductList: TObjectList;
    function GetProductList: TObjectList;
  public
    constructor Create;
    destructor Destroy; override;
    procedure AddProduct(aProduct:TProduct);
    property ProductList: TObjectList read GetProductList;
  end;
 

implementation

{
*********************************** TProduct ***********************************
}
constructor TProduct.Create;
begin
   FName:='商品TProduct';
  FPrice:=0;
  ShowMessage('商品被创建');
end;

function TProduct.GetName: string;
begin
  Result:=FName;
end;

function TProduct.GetPrice: Double;
begin
  Result:=FPrice;
end;

procedure TProduct.SayHello;
begin
end;

procedure TProduct.SetName(const Value: string);
begin
  if FName<>Value then
     FName:=Value
  else
     ;
end;

procedure TProduct.SetPrice(Value: Double);
begin
  If Value>0 then
     FPrice:=Value
  else
     FPrice:=0;
end;

{
********************************** TProductXY **********************************
}
procedure TProductXY.SayHello;
begin
  ShowMessage('我是西药,我的名字是'+Name+'单价:'+FloattoStr(Price));
end;

{
********************************** TProductZY **********************************
}
procedure TProductZY.SayHello;
begin
  ShowMessage('我是中药,我的名字是'+Name+'单价:'+FloattoStr(Price));
end;

{
************************************ TStore ************************************
}
constructor TStore.Create;
begin
  FProductList:=TObjectList.Create();
  FProductList.OwnsObjects := True;  //使FProductList在销毁时自动销毁其下拥有的对象
end;

destructor TStore.Destroy;
begin
  FProductList.Free;
  inherited Destroy;
end;

procedure TStore.AddProduct(aProduct:TProduct);
begin
  FProductList.Add(aProduct);
  ShowMessage(aProduct.Name+'被加入');
end;

function TStore.GetProductList: TObjectList;
begin
  Result:=FProductList;
end;


end.

你可能感兴趣的:(类)