虚拟方法问题

buffalo 2004-03-17 01:45:28
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TBase = Class(Tobject)
public procedure VM1;virtual;
public procedure VM2;virtual;
end;
TDerived1 = Class(TBase)
public procedure VM1;virtual;
// public procedure VM2;virtual;
end;
TDerived2 = Class(TDerived1)
public procedure VM1;virtual;
// public procedure VM2;virtual;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
aChild: TDerived2 ;
begin
aChild := TDerived2.Create;
try
aChild.VM1;
finally
aChild.Free;
end;
end;

{ TBase }

procedure TBase.VM1;
begin
showmessage('TBase.VM1');
end;

procedure TBase.VM2;
begin

end;


procedure TDerived2.VM1;
begin
inherited VM1;
showmessage('TDerived2.VM1');
end;

{ TDerived1 }

procedure TDerived1.VM1;
begin
showmessage('TDerived1.VM1');
end;

end.


现在我想继承自TDerived1类TDerived2调用Base.VM1,
但不想改动TDerived1,如何办到???
...全文
78 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
buffalo 2004-03-17
  • 打赏
  • 举报
回复
如果不知道基类,怎么办?
procedure TForm1.Button1Click(Sender: TObject);
var
aChild: TDerived2 ;
ac: TClass;
begin

aChild := TDerived2.Create;
ac := aChild.ClassParent.ClassParent;
// ShowMessage(ac.ClassName);
try
// Tbase(aChild).VM1;
finally
aChild.Free;
end;
end;

这个ac没什么用,有没有更灵活一点的办法?^_^
buffalo 2004-03-17
  • 打赏
  • 举报
回复
不错,接分!
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
var
aChild: TDerived2 ;
begin
aChild := TDerived2.Create;
try
tbase(aChild).VM1;
finally
aChild.Free;
end;
end;


try ^_^

16,749

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧