• inherited 的研究。


    结论:

    1. inherited默认调用的是父类的同名 同参数方法。(常用,如果是同名 同参数方法 比如 overide 的,可以省略,只写个inherited就可。)

    2. 子类的方法里可以 inherited+ 父类的其它非同名 同参数方法。见:下方 son3

    若父类不存在 同名 同参数方法 则编译报错。如下图:

    unit Unit6;
    
    interface
    
    uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
      Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Samples.Gauges;
    
    type
      TForm6 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
      TFather = class
        public
          constructor Create; overload;
          constructor Create(const str: string); overload;
      end;
    
      TSon = class(TFather)
        public
          constructor Create; overload;
          constructor Create(const str: string); overload;
          constructor Create(const str1, str2: string); overload;
      end;
    
    var
      Form6: TForm6;
    
    implementation
    
    {$R *.dfm}
    
    { TFather }
    
    constructor TFather.Create;
    begin
      OutputDebugString('father');
    end;
    
    constructor TFather.Create(const str: string);
    begin
      OutputDebugString(PChar(str));
    end;
    
    { TSon }
    
    constructor TSon.Create;
    begin
      inherited;
    end;
    
    constructor TSon.Create(const str: string);
    begin
      inherited;
    end;
    
    constructor TSon.Create(const str1, str2: string);
    begin
      //inherited; //父类没有两个参数的函数的时候会怎样?
      inherited Create(str1 + str2); //这里用了 非同名 同参数方法
    end;
    
    procedure TForm6.Button1Click(Sender: TObject);
    var
      son1, son2, son3: TSon;
    begin
      son1 := TSon.Create;
      son2 := TSon.Create('test');
      son3 := TSon.Create('abc', 'cde');
    
      son1.Free;
      son2.Free;
      son3.Free;
    end;
    
    end.
  • 相关阅读:
    iOS 网络编程:socket
    Zsh和Bash,究竟有何不同
    关于微软win10 2004的更新以及wsl2 Ubuntu18.04安装
    C# 委托事件机制 订阅发布
    关于Docker理念和安装,对Visual Studio2019自带生成的DockerFile配置,以及Docker镜像的发布与拉取
    div弹框
    vs分页
    ado显示下拉
    SQL server动态拼接存储过程分页
    退役
  • 原文地址:https://www.cnblogs.com/del88/p/6893493.html
Copyright © 2020-2023  润新知