zoukankan      html  css  js  c++  java
  • 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.
  • 相关阅读:
    Oracle配置监听
    Oracle创建表空间和分配用户权限
    Dijkstra
    【刷题】【dp】【记忆化搜索】单词游戏
    【刷题】【记忆化搜索】【dp】Longtail Hedgehog
    【刷题】【dp】 Make The Fence Great Again
    【技巧】【卡常】
    【二分】【基础】(跳石头)(合并果子)(蚯蚓)
    【笔记】两种交换元素的方案对比
    【刷题】【单调栈】请客
  • 原文地址:https://www.cnblogs.com/del88/p/6893493.html
Copyright © 2011-2022 走看看