结论:
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.
时间: 2024-11-11 18:03:46