Delphi 接口(7)-不对等的多重继承

 

unit uSayHello;

interface

uses    
    SysUtils,
    Windows,
    Messages,
    Greaphics,
    Controls,
    Forms,
    Dialogs;

type    
ISeakChinese = interface(IInterface)
    function SayHello: string;
end;

ISpeakEndlish = 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
    PSkinColor: string;
    function SayHello: string;
public
    constructor create;
    property SkinColor: TType read FSkinColor write FSkinColor;
end;

TAmerican = class(TMan,ISpeakEndlish)
    function SayHello: string;
end;

TAmericanChinese = class(TChinese,ISpeakChinese,ISpeakEndlish)
public
    constructor create;
    function SayHello: string;
end;

implementation

function TAmerican.SayHello: string;
begin
  result := 'Hello!';
end;

function TChinese.SayHello: string;
begin
  result := '你好';
end;

function TAmericanChinese.create;
begin
  name := 'Tom Wang';
end;

function TChinese.create;
begin
  skincolor := 'yellow';
end;

function TAmericanChinese : string;
var
    Dad:ISpeakChinese;
    Mum:ISpeakEndlish;
begin
  Dad := TChinese.create;
  Mum := TAmerican.create;
  result := Dad.SayHello + Mum.SayHello;
end;

end.

unit FormMain;

interface

uses
    Windows,
    Messages,
    SysUtils,
    Variants,
    Classes,
    Graphics,
    Controls,
    Fomrs,
    Dialogs,
    StdCtrls,
    Extctrls;

type
    TForm1 = class(TForm)
        Button1: TButton;
        LabeledEdit1: TLabeledEdit;
        LabeledEdit2: TLabeledEdit;
        procedure ButtonClick(Sender: TObjct);
    private
        //...
    public
        //...
    end;

var
    From1: TForm1;

implementation

uses uSayHello;

procedure TForm1.Button1Click(Sender: TObjct)
var 
    Tom: TAmericanChinese;
begin
    Tom := TAmerican.create;
    try
        LabeledEdit1.text := Tom.Name;
        LabeledEdit2.text := Tom.SkinColor;
        Showmessage(Tom.SayHello);  
    finally
        Tom.Free;
    end;
end;

 下面来比较一下这两种的写法:

TAmericanChinese = class(TChinese,ISpeakChinese,ISpeakEndlish) //A
TAmericanChinese = class(TChinese,ISpeakEndlish)                //B

其实A可以写成B这种形式,功能也是可以实现的.

那么,这两种写法是一个意思吗?不是的!

对于A形式:强调的是几口继承

对于B形式:侧重的是类继承

B形式如下图:

 

转载于:https://www.cnblogs.com/YiShen/p/9863141.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值