Delphi 接口

Posted yishen

tags:

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

技术分享图片

代码如下:

unit Unit2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Unit1, Vcl.StdCtrls;

type
  TForm2 = class(TForm)
    edt_Name: TEdit;
    edt_language: TEdit;
    edt_SkinColor: TEdit;
    lbl1: TLabel;
    lbl2: TLabel;
    lbl3: TLabel;
    btn1: TButton;
    btn2: TButton;
    btn3: TButton;
    btn4: TButton;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btn3Click(Sender: TObject);
    procedure btn4Click(Sender: TObject);
  private
    { Private declarations }
    procedure SayHello(AMan: TMan; G: IGreetable);
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

{ TForm2 }

procedure TForm2.btn1Click(Sender: TObject);
var
  G: IGreetable;
  AMan: TMan;
begin
  AMan:=TChinese.Create;
  G:=TChinese.Create;
  SayHello(AMan,g);
end;

procedure TForm2.btn2Click(Sender: TObject);
var
  G: IGreetable;
  AMan: TAmerican;
begin
  AMan := TAmerican.Create;
  G := TAmerican.Create;
  SayHello(AMan,G);
end;

procedure TForm2.btn3Click(Sender: TObject);
var
  G: IGreetable;
  AMan: TFrench;
begin
  AMan := TFrench.Create;
  G := TFrench.Create;
  SayHello(AMan,G);
end;

procedure TForm2.btn4Click(Sender: TObject);
var
  G: IGreetable;
  AMan: TKorean;
begin
  AMan := TKorean.Create;
  G := TKorean.Create;
  SayHello(AMan,G);
end;

procedure TForm2.SayHello(AMan: TMan; G: IGreetable);
begin
  edt_Name.Text := AMan.Name;
  edt_language.Text := AMan.Language;
  edt_SkinColor.Text := AMan.SkinColor;
end;

end.

Unit单元代码如下:

unit Unit1;

interface

uses
  Winapi.Windows, Messages, System.StrUtils, System.Variants, System.Classes,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  IGreetable = interface
    [{00000000-0000-0000-C000-000000000046}]
    //function SayHello: string;
    //function Eat:string;
  end;

  TMan = class(TInterfacedObject, IGreetable)
    Language:string;
    Married:Boolean;
    Name:string;
    SkinColor:string;
  private
    function _Release: integer;
    public
      constructor Create;virtual;
  end;

  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 TChinese.create;
begin
  inherited;

end;

function TChinese.SayHello: string;
begin
  Result := chinese.bmp;
end;

{ TMan }

constructor TMan.Create;
begin
  name := 張三;
  language := 中文;
  SkinColor := 黃色;
end;

function TMan._Release: integer;
begin

end;

{ TAmerican }

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

function TAmerican.SayHello: string;
begin
  Result := americam.bmp;
end;

{ TFrench }

constructor TFrench.create;
begin
  Name := SuFei;
  Language := 法文;
  SkinColor := white;
end;

function TFrench.SayHello: string;
begin
  Result := french.bmp;
end;

{ TKorean }

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

function TKorean.SayHello: string;
begin
  Result := korean.bmp;
end;

end.

窗体代码:

object Form2: TForm2
  Left = 0
  Top = 0
  Caption = Form2
  ClientHeight = 291
  ClientWidth = 306
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = Tahoma
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 120
  TextHeight = 16
  object lbl1: TLabel
    Left = 48
    Top = 8
    Width = 38
    Height = 16
    Caption = Name:
  end
  object lbl2: TLabel
    Left = 48
    Top = 46
    Width = 60
    Height = 16
    Caption = Language:
  end
  object lbl3: TLabel
    Left = 49
    Top = 80
    Width = 59
    Height = 16
    Caption = SkinColor:
  end
  object edt_Name: TEdit
    Left = 114
    Top = 8
    Width = 121
    Height = 24
    TabOrder = 0
    Text = edt_Name
  end
  object edt_language: TEdit
    Left = 114
    Top = 38
    Width = 121
    Height = 24
    TabOrder = 1
    Text = edt_language
  end
  object edt_SkinColor: TEdit
    Left = 114
    Top = 77
    Width = 121
    Height = 24
    TabOrder = 2
    Text = edt_SkinColor
  end
  object btn1: TButton
    Left = 114
    Top = 128
    Width = 121
    Height = 25
    Caption = chinese
    TabOrder = 3
    OnClick = btn1Click
  end
  object btn2: TButton
    Left = 114
    Top = 159
    Width = 121
    Height = 26
    Caption = American
    TabOrder = 4
    OnClick = btn2Click
  end
  object btn3: TButton
    Left = 114
    Top = 191
    Width = 121
    Height = 25
    Caption = French
    TabOrder = 5
    OnClick = btn3Click
  end
  object btn4: TButton
    Left = 114
    Top = 222
    Width = 121
    Height = 25
    Caption = Korean
    TabOrder = 6
    OnClick = btn4Click
  end
end

 

以上是关于Delphi 接口的主要内容,如果未能解决你的问题,请参考以下文章

使用delphi+intraweb进行微信开发1~4代码示例

Delphi7:快捷键

Delphi编写自定义控件以及接口的使用(做了一个TpgDbEdit)

从父片段到选项卡片段的接口侦听器不起作用

Delphi 接口

Delphi 接口