Delphi 接口-接口和面向对象编程

Posted yishen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Delphi 接口-接口和面向对象编程相关的知识,希望对你有一定的参考价值。

技术分享图片

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 接口-接口和面向对象编程的主要内容,如果未能解决你的问题,请参考以下文章

delphi 面向对象实用技能教学一(递归)

面向接口编程和面向实现编程的区别

面向接口设计和编程——(面向对象面向接口面向过程面向实现) --转载

关于java面向对象编程和面向接口编程区别

C#中抽象类与接口的区别

面向接口编程详解