码迷,mamicode.com
首页 > Windows程序 > 详细

Delphi 接口(9)-接口和面向对象编程

时间:2018-10-28 16:11:07      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:delphi   韩国   override   你好   程序   int   图片   接口   lis   

技术分享图片

unit uSayHello;

interface

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

type 

IGreetable = interface
    [|0000-0000-0000-0000|]
    function SayHello: PChar;
end;

TMan = class(TInterfacedObject,IGreetable)
    Language: string;
    Married: Boolean;
    Name: string;
    SkinColor: string;
    constructor create; virtual;
    function SayHello: PChar; virtual; abstract;
end;

TChinese = class(TMan)
public  
    constructor create; override;
private
    function SayHello: PChar; override;
end;

TAmerican = class(TMan)
public
    constructor create; override;
private 
    function SayHello: PChar; override;
end;

TFrnch = class(TMan)
public
    constructor create; override;
private
    function SayHello: pchar ;override;
end;

TKorean = class(TMan)
public
    constructor create; override;
private
    function SayHello: pchar ; override;
end;

TET = class(TInterfacedObject,IGreetable)
private
    function SayHello: pchar;
end;

TRobot = class(TInterfacedObject,Igreetable)
private
    function SayHello: pchar;
end;

implementation

constructor TMan.create;
begin
  Name := 张三;
  Language := 中文;
  SkinColor := yellow;
end;

constructor TChinese.create;
begin
    inherited;
end;

constructor TAmerican.create;
begin
    Name := Lee;
    Language := English;
    SkinColor := black;
end;

constructor TFrnch.create;
begin
    Name:=苏菲;
    Language := 法文;
    SkinColor := white;
end;

constructor TKorean.create;
begin
    Name := 韩国;
    Language := 韩文;
    SkinColor := yellow;
end;

function TChinese.SayHello;
begin
    result := 中国人:你好!;
end;

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

function TFrench.SayHello;
begin
    result := 法国人:你好;
end;

function TKorean.SayHello;
begin   
    result := 韩国人:你好;
end;

function TET.SayHello;
begin
    result := TET:你好;
end;

function TRobot.SayHello;
begin
    result := Robot:你好;
end;

unit uFromSayHello

uses    
    //...

type
TFormSayHello = class(TForm)
    btnUSA: TButton;
    btnKorean: TButton;
    btnCN: TButton;
    btnFrench: TButton;
    btnET: TButton;
    btnRobott: TButton;
    procedure btnUSAClick(Sender: TObject);
    procedure btnKoreanClick(Sender: TObject);
    procedure btnCNClick(Sender: TObject);
    procedure btnFrenchClick(Sender: TObject);
    procedure btnETClick(Sender: TObject);
    procedure btnRobottClick(Sender: TObject);
end;

procedure TFormSayHello.SayHello(greeting: IGreetable);
begin
    ShoeMessage(greeting.SayHello);
end;

procedure TFormSayHello.btnKoreanClick(Sender: TObject);
begin
    SayHello(TKorean.create);
end;

procedure TFormSayHello.btnCNClick(Sender: TObject);
begin
    SayHello(TChinese.create);
end;

procedure TFormSayHello.btnFrenchClick(Sender: TObject);
begin
    SayHello(TFrench.create);
end;

procedure TFormSayHello.btnETClick(Sender: TObject);
begin
    SayHello(TET.create);
end;

procedure TFormSayHello.btnRobottClick(Sender: TObject);
begin
    SayHello(TRobot.create);
end;

技术分享图片

技术分享图片

//进一步修改
unit uSayHello;

interface

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

type 

IGreetable = interface
    [|0000-0000-0000-0000|]
    function SayHello: PChar;
end;

TMan = class(TInterfacedObject,IGreetable)
    Language: string;
    Married: Boolean;
    Name: string;
    SkinColor: string;
    constructor create; virtual;
    function SayHello: PChar; virtual; abstract;
end;

TChinese = class(TMan)
public  
    constructor create; override;
private
    function SayHello: PChar; override;
end;

TAmerican = class(TMan)
public
    constructor create; override;
private 
    function SayHello: PChar; override;
end;

TFrnch = class(TMan)
public
    constructor create; override;
private
    function SayHello: pchar ;override;
end;

TKorean = class(TMan)
public
    constructor create; override;
private
    function SayHello: pchar ; override;
end;

TET = class(TInterfacedObject,IGreetable)
private
    function SayHello: pchar;
end;

TRobot = class(TInterfacedObject,Igreetable)
private
    function SayHello: pchar;
end;

implementation

constructor TMan.create;
begin
  Name := 张三;
  Language := 中文;
  SkinColor := yellow;
end;

constructor TChinese.create;
begin
    inherited;
end;

constructor TAmerican.create;
begin
    Name := Lee;
    Language := English;
    SkinColor := black;
end;

constructor TFrnch.create;
begin
    Name:=苏菲;
    Language := 法文;
    SkinColor := white;
end;

constructor TKorean.create;
begin
    Name := 韩国;
    Language := 韩文;
    SkinColor := yellow;
end;

function TChinese.SayHello;
begin
    result := 中国人:你好!;
end;

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

function TFrench.SayHello;
begin
    result := 法国人:你好;
end;

function TKorean.SayHello;
begin   
    result := 韩国人:你好;
end;

function TET.SayHello;
begin
    result := TET:你好;
end;

function TRobot.SayHello;
begin
    result := Robot:你好;
end;

unit uFromSayHello

uses    
    //...

type
TFormSayHello = class(TForm)
    btnUSA: TButton;
    btnKorean: TButton;
    btnCN: TButton;
    btnFrench: TButton;
    btnET: TButton;
    btnRobott: TButton;
    procedure btnUSAClick(Sender: TObject);
    procedure btnKoreanClick(Sender: TObject);
    procedure btnCNClick(Sender: TObject);
    procedure btnFrenchClick(Sender: TObject);
    procedure btnETClick(Sender: TObject);
    procedure btnRobottClick(Sender: TObject);
end;

procedure TFormSayHello.SayHello(greeting: IGreetable);
begin
    ShowMessage(greeting.SayHello);
end;

//人类问候
procedure TFormSayHello.SayHello(AMan: TMan)
begin
    editName.text := AMan.Name;
    editLanguage.text := AMan.Language;
    editSkinColor.text := AMan.SkinColor;
    ShowMessage(AMan.SayHello);
end;

//这里新增来自非人类的另类问候方式.
//通过方法重载,使得程序的改动降低到最少.
procedure TFormSayHello.SayHello(greeting: Igreetable)
begin
    editName.text := copy(greeting.SayHello,1,6);
    editLanguage.text := copy(greeting.SayHello,7,4);
    editSkinColor.text := copy(greeting.SayHello,11,6);
    ShowMessage(greeting.SayHello);
end;

procedure TFormSayHello.btnKoreanClick(Sender: TObject);
begin
    SayHello(TKorean.create);
end;

procedure TFormSayHello.btnCNClick(Sender: TObject);
begin
    SayHello(TChinese.create);
end;

procedure TFormSayHello.btnFrenchClick(Sender: TObject);
begin
    SayHello(TFrench.create);
end;

procedure TFormSayHello.btnETClick(Sender: TObject);
begin
    SayHello(TET.create);
end;

procedure TFormSayHello.btnRobottClick(Sender: TObject);
begin
    SayHello(TRobot.create);
end;

技术分享图片

技术分享图片

 

Delphi 接口(9)-接口和面向对象编程

标签:delphi   韩国   override   你好   程序   int   图片   接口   lis   

原文地址:https://www.cnblogs.com/YiShen/p/9865669.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!