如何通过Delphi调用自定义的爷爷类对象中的方法?
- 内容介绍
- 文章标签
- 相关推荐
本文共计353个文字,预计阅读时间需要2分钟。
Delphi中通过inherited可以调用父类的方法,但无法直接调用父类的父类(即祖父类)的方法。可以通过传递的方式来间接实现。例如:
假设有父类TFather和祖父类TGrand,要调用祖父类的Write方法,可以这样做:delphi// 父类TFatherTFather=class procedure Write; virtual;end;
// 祖父类TGrandTGrand=class procedure Write;end;
// 子类继承自TFatherTMyClass=class(TFather) procedure Write; override;end;
// 子类中的Write方法procedure TMyClass.Write;begin inherited Write; // 调用父类TFather的Write方法 TGrand.Write; // 直接调用祖父类TGrand的Write方法end;
// 祖父类TGrand的Write方法procedure TGrand.Write;begin // 写入操作end;
Delphi通过inherited 可以调用父类的方法,但是没有提供直接调用父类的父类的方法(爷爷类),通过变通的方式实现如下:
假设父类是TFather,爷爷类TGrand,调用爷爷类的Write方法:
1 2 3 4 5 6 7 8 9type
TWriteProc=
procedure
of
Object
;
var
WriteProc:TWriteProc;
....
TMethod(WriteProc).Code:[emailprotected]
.
Write
;
TMethod(WriteProc).Data:=Self;
WriteProc;
www.cnblogs.com/hezihang/p/4139656.html
本文共计353个文字,预计阅读时间需要2分钟。
Delphi中通过inherited可以调用父类的方法,但无法直接调用父类的父类(即祖父类)的方法。可以通过传递的方式来间接实现。例如:
假设有父类TFather和祖父类TGrand,要调用祖父类的Write方法,可以这样做:delphi// 父类TFatherTFather=class procedure Write; virtual;end;
// 祖父类TGrandTGrand=class procedure Write;end;
// 子类继承自TFatherTMyClass=class(TFather) procedure Write; override;end;
// 子类中的Write方法procedure TMyClass.Write;begin inherited Write; // 调用父类TFather的Write方法 TGrand.Write; // 直接调用祖父类TGrand的Write方法end;
// 祖父类TGrand的Write方法procedure TGrand.Write;begin // 写入操作end;
Delphi通过inherited 可以调用父类的方法,但是没有提供直接调用父类的父类的方法(爷爷类),通过变通的方式实现如下:
假设父类是TFather,爷爷类TGrand,调用爷爷类的Write方法:
1 2 3 4 5 6 7 8 9type
TWriteProc=
procedure
of
Object
;
var
WriteProc:TWriteProc;
....
TMethod(WriteProc).Code:[emailprotected]
.
Write
;
TMethod(WriteProc).Data:=Self;
WriteProc;
www.cnblogs.com/hezihang/p/4139656.html

