02.Delphi通过接口实现多重继承
uSayHello类如下:
unit uSayHello; interface type
// 接口
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; implementation constructor 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; interface uses
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.
02.Delphi通过接口实现多重继承的更多相关文章
- 03.Delphi通过接口实现多重继承的优化
在上一篇02中,写到的sayhello函数,需要使用2个接口参数,很繁琐.可以使用as参数,把多重继承的子类对象变成需要的对象 uSayHello代码如下 unit uSayHello; interf ...
- Delphi 的接口机制——接口操作的编译器实现过程(1)
学习COM编程技术也快有半个月了,这期间看了很多资料和别人的程序源码,也尝试了用delphi.C++.C#编写COM程序,个人感觉Delphi是最好上手的.C++的模版生成的代码太过复杂繁琐,大量使用 ...
- delphi中接口的委托和聚合
Delphi的TRegistry注册表类 方法详解 Delphi的接口编程入门 delphi中接口的委托和聚合 2009-09-27 10:44:44| 分类: 默认分类 | 标签: |举报 |字 ...
- 05.Delphi接口的多重继承深入
由于是IInterface,申明了SayHello,需要由继承类来实现函数,相对于03篇可以再精简一下 unit uSayHello; interface uses SysUtils, Windows ...
- 04.Delphi通过接口IInterface实现多重继承
IInterface表示申明了一些函数,自己本身没有实现部分,需要由继承它的类来实现函数 uSayHello代码如下 unit uSayHello; interface uses SysUtils, ...
- C++解析(24):抽象类和接口、多重继承
0.目录 1.抽象类和接口 1.1 抽象类 1.2 纯虚函数 1.3 接口 2.被遗弃的多重继承 2.1 C++中的多重继承 2.2 多重继承的问题一 2.3 多重继承的问题二 2.4 多重继承的问题 ...
- Delphi面向对象---接口
从Delphi3开始支持接口.接口定义了能够与一个对象进行交互操作的一组过程和函数.对一个接口进行定义包含两个方面的内容: 1)一方面是实现这个接口 2)另一方面是定义接口的客户 一个类能够实现多个接 ...
- java中接口与多重继承的关系
在Java语言中, abstract class 和interface 是支持抽象类定义的两种机制.正是由于这两种机制的存在,才赋予了Java强大的 面向对象能力.abstract class和int ...
- Delphi 的接口机制——接口操作的编译器实现过程(2)
接口对象的内存空间 假设我们定义了如下两个接口 IIntfA 和 IIntfB,其中 ProcA 和 ProcB 将实现为静态方法,而 VirtA 和 VirtB 将以虚方法实现: IIntfA = ...
随机推荐
- GitHub 网站汉化
居然是一个中文Github网站!该不会是个假的吧? 2018-09-03 17:30 前几天分享了一篇文章——3个搜索技巧!在 GitHub上快速找到实用资源!眼尖心细的读者发现了文中的Github网 ...
- 【剑指Offer面试编程题】题目1360:乐透之猜数游戏--九度OJ
题目描述: 六一儿童节到了,YZ买了很多丰厚的礼品,准备奖励给JOBDU里辛劳的员工.为了增添一点趣味性,他还准备了一些不同类型的骰子,打算以掷骰子猜数字的方式发放奖品.例如,有的骰子有6个点数(点数 ...
- IELTS Simon wr task p3
- tcp连接建立和断开
TCP协议作为传输层主要协议之一,具有面向连接,端到端,可靠的全双工通信,面向字节流的数据传输协议. 1.TCP报文段 虽然TCP面试字节流,但TCP传输的数据单元却是报文段.TCP报文段分为TCP首 ...
- Linux centosVMware Tomcat介绍、安装jdk、安装Tomcat
一.Tomcat介绍 Tomcat是Apache软件基金会(Apache Software Foundation)的Jakarta项目中的一个核心项目,由Apache.Sun和其他一些公司及个人共同开 ...
- WebMagic基础与Maven管理依赖
2. 快速开始 WebMagic主要包含两个jar包:webmagic-core-{version}.jar和webmagic-extension-{version}.jar.在项目中添加这两个包的依 ...
- FFmpeg——AVFrame中 的 data
AVFrame中 的 data 的定义如下: typedef struct AVFrame { #define AV_NUM_DATA_POINTERS 8 /** * pointer to the ...
- redis服务器性能优化
1.系统内存OOM优化 vm.overcommit_memory Redis会占用非常大内存,所以通常需要关闭系统的OOM,方法为将“/proc/sys/vm/overcommit_memory”的值 ...
- 源码安装openldap(转)
Ubuntu安装OpenLDAP(附错误的详细解决办法) 1 下载OpenLDAP源码 http://www.openldap.org/software/download/ 或者 ftp://ftp. ...
- java set的线程安全
CopyOnWriteArraySet和ConcurrentSkipListSet 与线程不安全的集合类的对应关系 HashSet -> CopyOnWriteArraySet TreeSet ...