zoukankan      html  css  js  c++  java
  • 06.Delphi接口的不对等的多重继承

    uSayHello代码如下

    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
        FSkinColor: string;
        function SayHello: string;
      public
        constructor create;
        property SkinColor: string read FSkinColor write FSkinColor;
      end;
    
      TAmerican = class(TMan, ISpeakEnglish)
      private
        function SayHello: string;
      end;
    
      TAmericanChinese = class(TChinese, ISpeakEnglish)
      public
        constructor create;
        function TomSayHello: string;
      end;
    
    implementation
    
    function TAmerican.SayHello: string;
    begin
      result := 'Hello!';
    end;
    
    constructor TChinese.create;
    begin
      SkinColor := '黄色';
    end;
    
    function TChinese.SayHello: string;
    begin
      result := '你好!';
    end;
    
    constructor TAmericanChinese.create;
    begin
      name := 'Tom Wang';
      Inherited;
    end;
    
    function TAmericanChinese.TomSayHello: string;
    var
      // Dad: ISpeakChinese;
      Mum: ISpeakEnglish;
    begin
      // Dad:=TChinese.Create;
      // 只有子类TAmerican才具体实现了SayHello的方法
      Mum := TAmerican.create;
      // SayHello是继承的中国人的Hello方法
      result := 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;
        LabeledEdit2: TLabeledEdit;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    uses uSayHello;
    
    {$R *.dfm}
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      Tom:TAmericanChinese;
    begin
       Tom:=TAmericanChinese.Create;
      try
        LabeledEdit1.text:=Tom.Name;
        LabeledEdit2.text:=Tom.SkinColor;
        Showmessage(Tom.Tomsayhello);
      finally
        Tom.Free;
      end;
    end;
    
    end.
  • 相关阅读:
    支付宝 微信支付 移动支付 网站支付 开发
    2017 开源中国评比的前100个优秀开源项目
    解决error: Your local changes to the following files would be overwritten by merge
    Spring-JDBC配置
    server library[unbound] 服务未绑定解决办法
    MyEclipse安装EGit插件方法
    使用GitHub和Eclipse进行javaEE开发步骤
    Spring-AOP
    SQL-字符串连接聚合函数
    Spring-注入外部值
  • 原文地址:https://www.cnblogs.com/tianpan2019/p/11482270.html
Copyright © 2011-2022 走看看