zoukankan      html  css  js  c++  java
  • 覆盖虚方法

    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs;
    
    type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    //夫类
      TParent = class
      protected
        function MyFun(i: Integer): Integer; dynamic;  //动态方法
        procedure MyProc; virtual;  //虚方法
      end;
    
    //子类
      TChild = class(TParent)
      protected
        function MyFun(i: Integer): Integer; override;  //覆盖
        procedure MyProc; override;  //覆盖
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    { TParent }
    
    function TParent.MyFun(i: Integer): Integer;
    begin
      Inc(i);
      Result := i;
    end;
    
    procedure TParent.MyProc;
    begin
      ShowMessage('Parent');
    end;
    
    { TChild }
    
    function TChild.MyFun(i: Integer): Integer;
    begin
      i := inherited MyFun(i);  //先调用夫类方法,被 +1;当然可以不调用
      Inc(i);                   //子类再 +1
      Result := i;
    end;
    
    procedure TChild.MyProc;
    begin
      inherited;  //先调用夫类方法;当然可以不调用
      ShowMessage('Child');
    end;
    
    //测试
    procedure TForm1.FormCreate(Sender: TObject);
    var
      p: TParent;
      c: TChild;
    begin
      p := TParent.Create;
      c := TChild.Create;
    
      p.MyProc;  //Parent
      c.MyProc;  //Parent; TChild
    
      ShowMessage(IntToStr(p.MyFun(2)));  //3
      ShowMessage(IntToStr(c.MyFun(2)));  //4
    
      p.Free;
      c.Free;
    end;
    
    end.
    
  • 相关阅读:
    Javascript 正确用法 二
    c# 未能载入文件或程序集
    Linux系统备份
    环保创业的可行之道——Leo鉴书上66
    Oracle的序列
    UVA 10574
    网页内容的html标签补全和过滤的两种方法
    使用POI来实现对Excel的读写操作
    OVER(PARTITION BY)函数介绍
    Kill 正在执行的存储过程
  • 原文地址:https://www.cnblogs.com/del/p/983657.html
Copyright © 2011-2022 走看看