覆盖虚方法
unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs;typeTForm1 = class(TForm)procedure FormCreate(Sender: TObject);private{ Private declarations }public{ Public declarations }end;//夫類TParent = classprotectedfunction MyFun(i: Integer): Integer; dynamic; //動態方法procedure MyProc; virtual; //虛方法end;//子類TChild = class(TParent)protectedfunction MyFun(i: Integer): Integer; override; //覆蓋procedure MyProc; override; //覆蓋end;varForm1: TForm1;implementation{$R *.dfm}{ TParent }function TParent.MyFun(i: Integer): Integer;
beginInc(i);Result := i;
end;procedure TParent.MyProc;
beginShowMessage('Parent');
end;{ TChild }function TChild.MyFun(i: Integer): Integer;
begini := inherited MyFun(i); //先調用夫類方法,被 +1;當然可以不調用Inc(i); //子類再 +1Result := i;
end;procedure TChild.MyProc;
begininherited; //先調用夫類方法;當然可以不調用ShowMessage('Child');
end;//測試
procedure TForm1.FormCreate(Sender: TObject);
varp: TParent;c: TChild;
beginp := TParent.Create;c := TChild.Create;p.MyProc; //Parentc.MyProc; //Parent; TChildShowMessage(IntToStr(p.MyFun(2))); //3ShowMessage(IntToStr(c.MyFun(2))); //4p.Free;c.Free;
end;end.
總結
- 上一篇: DirectX 9高层着色语言介绍3——
- 下一篇: ASP.NET 验证码示例