TPerson、ICustomer分别是对人和公司的封装,TPersonalCustomer从它俩继承,封装个人类型的客户(对应的还有公司类型的客户),TPersonalCustomer重载了TPerson类中的Name属性,但是在给Name赋值的时候,却引发了Out of Memory错误,为什么呢?代码片段://// Classes and Interface
type
TPerson = class;
TPersonalCustomer = class;
ICustomer = interface
function GetDisplayName: WideString; stdcall;
property DisplayName: WideString read GetDisplayName;
end; TPerson = class(TInterfacedObject, IInterface)
private
FName: WideString;
procedure SetName(const Value: WideString);
published
property Name: WideString read FName write SetName;
end; TPersonalCustomer = class(TPerson, ICustomer)
private
FNoshow: Boolean;
public
function GetDisplayName: WideString; stdcall;
published
property Name;
end;implementationuses SysUtils,
Dialogs;procedure TPerson.SetName(const Value: WideString);
begin
//ShowMessage(Value);
FName := Value; <-- out of memory here
end;function TPersonalCustomer.GetDisplayName: WideString;
begin
Result :='客户'+ Name;
end;end.
////// codes in the formprocedure TForm1.btn1Click(Sender: TObject);
var
Customer : ICustomer;
begin
Customer := TPersonalCustomer.Create;
TPerson(Customer).Name := widestring(edt1.Text );
ShowMessage(Customer.DisplayName);
Customer := nil;
end;
type
TPerson = class;
TPersonalCustomer = class;
ICustomer = interface
function GetDisplayName: WideString; stdcall;
property DisplayName: WideString read GetDisplayName;
end; TPerson = class(TInterfacedObject, IInterface)
private
FName: WideString;
procedure SetName(const Value: WideString);
published
property Name: WideString read FName write SetName;
end; TPersonalCustomer = class(TPerson, ICustomer)
private
FNoshow: Boolean;
public
function GetDisplayName: WideString; stdcall;
published
property Name;
end;implementationuses SysUtils,
Dialogs;procedure TPerson.SetName(const Value: WideString);
begin
//ShowMessage(Value);
FName := Value; <-- out of memory here
end;function TPersonalCustomer.GetDisplayName: WideString;
begin
Result :='客户'+ Name;
end;end.
////// codes in the formprocedure TForm1.btn1Click(Sender: TObject);
var
Customer : ICustomer;
begin
Customer := TPersonalCustomer.Create;
TPerson(Customer).Name := widestring(edt1.Text );
ShowMessage(Customer.DisplayName);
Customer := nil;
end;
Customer: ICustomer;
定义对象错拉