打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
02.Delphi通过接口实现多重继承

uSayHello类如下:

unit uSayHello;interfacetype  // 接口  IGreetable = interface    ['{D91DDE09-0FC4-4FE9-AE0D-9877E2F73BF6}']    // 输出函数    function SayHello: string;  end;  // TInterfacedObject实现了接口的默认方法  TMan = class(TInterfacedObject)    // 语言,姓名,皮肤颜色 属性    Language: string;    Name: string;    SkinColor: string;  public    // 虚方法virtual, 子类需要使用override来覆盖    constructor create; virtual;  end;  // 通过接口,继承了TMan的Create同时也继承了TGreetable的SayHello  TChinese = class(TMan, IGreetable)  public    constructor create; override;  private    function SayHello: string;  end;  TAmerican = class(TMan, IGreetable)  public    constructor create; override;  private    function SayHello: string;  end;  TFrench = class(TMan, IGreetable)  public    constructor create; override;  private    function SayHello: string;  end;  TKorean = class(TMan, IGreetable)  public    constructor create; override;  private    function SayHello: string;  end;implementationconstructor TMan.create;begin  Name := '张三';  Language := '中文';  SkinColor := '黄色';end;constructor TChinese.create;begin  inherited;end;constructor TAmerican.create;begin  Name := 'Lee';  Language := '英文';  SkinColor := '黑色';end;constructor TFrench.create;begin  Name := '苏菲';  Language := '法文';  SkinColor := '白色';end;constructor TKorean.create;begin  Name := '金知中';  Language := '韩文';  SkinColor := '黄色';end;function TChinese.SayHello;begin  Result := 'chinese.bmp';end;function TAmerican.SayHello;begin  Result := 'American.bmp';end;function TFrench.SayHello;begin  Result := 'French.bmp';end;function TKorean.SayHello;begin  Result := 'Korean.bmp';end;end.

界面代码如下,接口调用函数的参数,使用父内函数。接口传参的时候,用子类传参数。

unit ufrmSayHello;interfaceuses  Windows,  Messages,  SysUtils,  Variants,  Classes,  Graphics,  Controls,  Forms,  Dialogs,  StdCtrls,  ExtCtrls,  uSayHello;type  TfrmSayHello = class(TForm)    GroupBox1: TGroupBox;    edtName: TLabeledEdit;    edtSkinColor: TLabeledEdit;    edtLanguage: TLabeledEdit;    btnUSA: TButton;    btnKorean: TButton;    btnCN: TButton;    btnFrench: TButton;    Image1: TImage;    procedure btnUSAClick(Sender: TObject);    procedure btnCNClick(Sender: TObject);    procedure btnFrenchClick(Sender: TObject);    procedure btnKoreanClick(Sender: TObject);  private    procedure sayhello(AMan: TMan; G: IGreetable);  public    { Public declarations }  end;var  frmSayHello: TfrmSayHello;implementation{$R *.dfm}// 多个不同参数的函数,集成到了一个procedure TfrmSayHello.sayhello(AMan: TMan; G: IGreetable);begin  // 类实现的多态  edtName.Text := AMan.Name;  edtLanguage.Text := AMan.Language;  edtSkinColor.Text := AMan.SkinColor;  // 接口实现的多态  Image1.Picture.LoadFromFile(G.sayhello);end;procedure TfrmSayHello.btnUSAClick(Sender: TObject);var  G: IGreetable;  AMan: TMan;begin  // 按照父类定义,子类创建  AMan := TAmerican.create;  G := TAmerican.create;  sayhello(AMan, G);end;procedure TfrmSayHello.btnCNClick(Sender: TObject);begin  // sayhello很神奇的地方在于,不同的类型参数都可以传过去,不用因为参数不同,就需要声明不同的sayhello函数  sayhello(TChinese.create, TChinese.create);end;procedure TfrmSayHello.btnFrenchClick(Sender: TObject);begin  sayhello(TFrench.create, TFrench.create);end;procedure TfrmSayHello.btnKoreanClick(Sender: TObject);begin  sayhello(TKorean.create, TKorean.create);end;end.

 

来源:https://www.icode9.com/content-4-441101.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Delphi XE5 for Android (三)
TClientDataSet[16]: 用计算字段显示 UpdateStatus
delphi clipbrd剪贴板的读写和清除功能
Delphi 中使长循环有响应
Delphi exe实例之间传递cmd参数
WinAPI: PtInRect - 判断点是否在矩形中
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服