首页 > 其他 > 详细

inherited 的研究。

时间:2017-05-23 12:47:56      阅读:266      评论:0      收藏:0      [点我收藏+]

结论:

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.

 

inherited 的研究。

原文:http://www.cnblogs.com/del88/p/6893493.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!