D
dongnan99
Unregistered / Unconfirmed
GUEST, unregistred user!
我的问题是当一个类实现多个接口时,接口中有同名方法。是否必须在实现类中为同名方法指定别名,然后再实现???可下面的代码中TAmericanChinese 类实现ISpeakChinese, ISpeakEnglish接口却没有为其中的同名方法指定别名,只写了一个SayHello,这是什么意思??另外就是,下面的接口都没有guid,只不过继承了IInterface接口,是否这样写就可以不要guid,这样写有什么好处? 谢谢
unit uSayHello;
interface
uses
SysUtils, Windows, Messages, Classes, Graphics, Controls,
Forms, Dialogs;
type
ISpeakChinese = interface (IInterface)
function SayHello: string;
end;
ISpeakEnglish = interface (IInterface)
function SayHello: string;
end;
TMan = class (TInterfacedObject)
private
FName: string;
public
property Name: string read FName write FName;
end;
TChinese = class (TMan, ISpeakChinese)
private
function SayHello: string;
end;
TAmerican = class (TMan, ISpeakEnglish)
private
function SayHello: string;
end;
TAmericanChinese = class (TMan, ISpeakChinese, ISpeakEnglish)
public
constructor create;
function SayHello: string;
end;
implementation
{
********************************** TAmerican ***********************************
}
function TAmerican.SayHello: string;
begin
result:='Hello!';
end;
{
*********************************** TChinese ***********************************
}
function TChinese.SayHello: string;
begin
result:='你好!';
end;
{
******************************* TAmericanChinese *******************************
}
constructor TAmericanChinese.create;
begin
name:='Tom Wang';
end;
function TAmericanChinese.SayHello: string;
var
Dad: ISpeakChinese;
Mum: ISpeakEnglish;
begin
Dad:=TChinese.Create;
Mum:=TAmerican.Create;
result:=Dad.SayHello+Mum.SayHello;
end;
end.
unit uSayHello;
interface
uses
SysUtils, Windows, Messages, Classes, Graphics, Controls,
Forms, Dialogs;
type
ISpeakChinese = interface (IInterface)
function SayHello: string;
end;
ISpeakEnglish = interface (IInterface)
function SayHello: string;
end;
TMan = class (TInterfacedObject)
private
FName: string;
public
property Name: string read FName write FName;
end;
TChinese = class (TMan, ISpeakChinese)
private
function SayHello: string;
end;
TAmerican = class (TMan, ISpeakEnglish)
private
function SayHello: string;
end;
TAmericanChinese = class (TMan, ISpeakChinese, ISpeakEnglish)
public
constructor create;
function SayHello: string;
end;
implementation
{
********************************** TAmerican ***********************************
}
function TAmerican.SayHello: string;
begin
result:='Hello!';
end;
{
*********************************** TChinese ***********************************
}
function TChinese.SayHello: string;
begin
result:='你好!';
end;
{
******************************* TAmericanChinese *******************************
}
constructor TAmericanChinese.create;
begin
name:='Tom Wang';
end;
function TAmericanChinese.SayHello: string;
var
Dad: ISpeakChinese;
Mum: ISpeakEnglish;
begin
Dad:=TChinese.Create;
Mum:=TAmerican.Create;
result:=Dad.SayHello+Mum.SayHello;
end;
end.