04.Delphi通过接口IInterface实现多重继承
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了04.Delphi通过接口IInterface实现多重继承,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2294字,纯文字阅读大概需要4分钟。
内容图文
IInterface表示申明了一些函数,自己本身没有实现部分,需要由继承它的类来实现函数
uSayHello代码如下
unit uSayHello; interface uses SysUtils, Windows, Messages, Classes, Graphics, Controls, Forms, Dialogs; type // IInterface表示申明了一些函数,自己本身没有实现部分,需要由继承它的类来实现函数 ISpeakChinese = interface(IInterface) function SayHello: string; end; // IInterface表示申明了一些函数,自己本身没有实现部分,需要由继承它的类来实现函数 ISpeakEnglish = interface(IInterface) function SayHello: string; end; // 人类 TMan = class(TInterfacedObject) private// 姓名 FName: string; publicproperty Name: stringread FName write FName; end; // 中国人,继承了人类和说汉语 TChinese = class(TMan, ISpeakChinese) private// ISpeakChinese中定义的函数,序号在继承类中实现function SayHello: string; end; TAmerican = class(TMan, ISpeakEnglish) private// ISpeakChinese中定义的函数,序号在继承类中实现function SayHello: string; end; // 美籍华人 ,继承了人类,汉语和英语 TAmericanChinese = class(TMan, ISpeakChinese, ISpeakEnglish) publicconstructor create; // ISpeakChinese中定义的函数,序号在继承类中实现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 frmMain; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TForm1 = class(TForm) Button1: TButton; LabeledEdit1: TLabeledEdit; procedure Button1Click(Sender: TObject); private{ Private declarations }public{ Public declarations }end; var Form1: TForm1; implementationuses uSayHello; {$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); var Tom: TAmericanChinese; begin// 创建美籍华人类 Tom := TAmericanChinese.Create; try LabeledEdit1.text := Tom.Name; // 输出 ShowMessage(Tom.sayhello); finally Tom.Free; end; end; end.
原文:https://www.cnblogs.com/tianpan2019/p/11473979.html
内容总结
以上是互联网集市为您收集整理的04.Delphi通过接口IInterface实现多重继承全部内容,希望文章能够帮你解决04.Delphi通过接口IInterface实现多重继承所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。