• Inherited用法代码示例


    unit Unit1;

    interface

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

    type
      TForm1 = class(TForm)
        btn1: TButton;
        btn2: TButton;
        btn3: TButton;
        procedure btn1Click(Sender: TObject);
        procedure btn2Click(Sender: TObject);
        procedure btn3Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
      THuman=class   //基类
      public
      procedure showinfo1;virtual;
      procedure showinfo2;virtual;
      end;
      TStudent=class(Thuman)   //派生类
      public
      procedure showinfo1;override;//   虚拟继承
      procedure showinfo2;override;
      end;
      TWorker=Class(THuman)   //派生类
      public
      procedure showinfo1;override;
      procedure showinfo2;override;
      end;


    var
      Form1: TForm1;

    implementation

    {$R *.dfm}
    procedure THuman.showinfo1;
    begin
    ShowMessage('THuman   Showinfo1');
    end;
    procedure THuman.showinfo2;
    begin
    ShowMessage('THuman   Showinfo2');
    end;
    procedure TStudent.showinfo1;
    begin
    ShowMessage('TStudent   Showinfo1');
    inherited showinfo2;   //调用父类中不同名的成员函数
    end;
    procedure TStudent.showinfo2;
    begin
    ShowMessage('TStudent   Showinfo2');
    end;
    procedure TWorker.showinfo1;
    begin
    ShowMessage('TWorker   Showinfo1');
    inherited;   //调用父类中的同名函数
    end;
    procedure TWorker.showinfo2;
    begin
    ShowMessage('TWorker   Showinfo2');
    end;
    procedure TForm1.btn1Click(Sender: TObject);
    var
    H:THuman;
    begin
    H:=THuman.Create;
    h.showinfo1;
    h.showinfo2;
    end;

    //运行结果'THuman   Showinfo1'

    //然后'THuman   Showinfo2'
    procedure TForm1.btn2Click(Sender: TObject);
    var
    S:TStudent;
    begin
    s:=TStudent.Create;
    s.showinfo1;
    s.showinfo2;
    end;

    //运行结果'TStudent   Showinfo1'

    //然后'THuman   Showinfo2'

    //然后'TStudent   Showinfo2'


    procedure TForm1.btn3Click(Sender: TObject);
    var
    W:TWorker;
    begin
    w:=Tworker.Create;
    w.showinfo1;
    w.showinfo2;
    end;

    //运行结果'TWorker   Showinfo1'

    //然后'THuman   Showinfo1'

    //'TWorker   Showinfo2'
    end.

  • 相关阅读:
    AngularJS中写一个包裹HTML元素的directive
    AngularJS中实现Model缓存
    控制台获取AngularJS某个元素的Scope
    体验jQuery和AngularJS的不同点以及AngularJS的迷人之处
    AngularJS中$interval的用法
    AngularJS中处理多个promise
    AngularJS中获取数据源的几种方式
    AngularJS中ui-router全攻略
    AnguarJS中链式的一种更合理写法
    AngularJS转换请求内容
  • 原文地址:https://www.cnblogs.com/feng801/p/1336429.html
Copyright © 2020-2023  润新知