I
iforward
Unregistered / Unconfirmed
GUEST, unregistred user!
unit IntfUnit;
interface
uses
Classes, SysUtils, Dialogs;
type
IFormattedNumber = interface
['{2DE825C1-EADF-11D2-B39F-0040F67455FE}']
function FormattedString: string;
function GetName: string;
end;
TFormattedInteger = class(TInterfacedObject, IFormattedNumber)
private
FValue: Integer;
public
constructor Create(AValue: Integer);
destructor Destroy
override;
function FormattedString: string;
function GetName: string;
end;
TFormattedHexInteger = class(TFormattedInteger, IFormattedNumber)
public
destructor Destroy
override;
function FormattedString: string;
function GetName: string;
end;
implementation
uses
MainForm;
{ TFormattedInteger }
constructor TFormattedInteger.Create(AValue: Integer);
begin
inherited Create;
FValue := AValue;
end;
destructor TFormattedInteger.Destroy;
begin
Form1.Memo1.Lines.Add('TFormattedInteger.Destroy');
inherited Destroy;
end;
function TFormattedInteger.FormattedString: string;
begin
Result := 'The integer is ' + IntToStr(FValue);
end;
function TFormattedInteger.GetName: string;
begin
Result := 'TFormattedInteger.GetName';
end;
{ TFormattedHexInteger }
destructor TFormattedHexInteger.Destroy;
begin
Form1.Memo1.Lines.Add('TFormattedHexInteger.Destroy');
inherited Destroy;
end;
function TFormattedHexInteger.FormattedString: string;
begin
Result := 'The hex integer is $' + IntToHex(FValue, 4);
end;
function TFormattedHexInteger.GetName: string;
begin
Result := 'TFormattedHexInteger.GetName';
end;
end.
//////////////////////////////////////////////////////////
下面是我在窗体单元中的使用
procedure TForm1.Button1Click(Sender: TObject);
var
MyObject:Tobject;
MyNumber:IFormattedNumber;
begin
MyObject:=TFormattedInteger.Create(2);
MyNumber:=MyObject as IFormattedNumber
//此处编译器提示有错误,我是按书上来写的,为什么啊
ShowMessage(MyNumber.FormattedString);
end;
谢谢!
interface
uses
Classes, SysUtils, Dialogs;
type
IFormattedNumber = interface
['{2DE825C1-EADF-11D2-B39F-0040F67455FE}']
function FormattedString: string;
function GetName: string;
end;
TFormattedInteger = class(TInterfacedObject, IFormattedNumber)
private
FValue: Integer;
public
constructor Create(AValue: Integer);
destructor Destroy
override;
function FormattedString: string;
function GetName: string;
end;
TFormattedHexInteger = class(TFormattedInteger, IFormattedNumber)
public
destructor Destroy
override;
function FormattedString: string;
function GetName: string;
end;
implementation
uses
MainForm;
{ TFormattedInteger }
constructor TFormattedInteger.Create(AValue: Integer);
begin
inherited Create;
FValue := AValue;
end;
destructor TFormattedInteger.Destroy;
begin
Form1.Memo1.Lines.Add('TFormattedInteger.Destroy');
inherited Destroy;
end;
function TFormattedInteger.FormattedString: string;
begin
Result := 'The integer is ' + IntToStr(FValue);
end;
function TFormattedInteger.GetName: string;
begin
Result := 'TFormattedInteger.GetName';
end;
{ TFormattedHexInteger }
destructor TFormattedHexInteger.Destroy;
begin
Form1.Memo1.Lines.Add('TFormattedHexInteger.Destroy');
inherited Destroy;
end;
function TFormattedHexInteger.FormattedString: string;
begin
Result := 'The hex integer is $' + IntToHex(FValue, 4);
end;
function TFormattedHexInteger.GetName: string;
begin
Result := 'TFormattedHexInteger.GetName';
end;
end.
//////////////////////////////////////////////////////////
下面是我在窗体单元中的使用
procedure TForm1.Button1Click(Sender: TObject);
var
MyObject:Tobject;
MyNumber:IFormattedNumber;
begin
MyObject:=TFormattedInteger.Create(2);
MyNumber:=MyObject as IFormattedNumber
//此处编译器提示有错误,我是按书上来写的,为什么啊
ShowMessage(MyNumber.FormattedString);
end;
谢谢!