Delphi如何通过procedure of Object调用爷爷类的方法,实现跨类方法调用?

2026-04-10 22:032阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计454个文字,预计阅读时间需要2分钟。

Delphi如何通过procedure of Object调用爷爷类的方法,实现跨类方法调用?

通过继承使用Delphi中的`inherited`关键字可以调用父类的方法,但无法直接调用父类的父类(即祖父类)的方法。可以通过传递的方式来间接实现。以下是一个示例:

假设我们有三个类:`TFather`、`TGrand`和`TMyClass`。

delphi// 祖父类TGrand=classpublic procedure Write; virtual;end;

// 父类TFather=class(TGrand)public procedure Write; override;end;

// 子类TMyClass=class(TFather)public procedure Write; override;end;

// 实现祖父类的Write方法procedure TGrand.Write;begin WriteLn('Grand class method called.');end;

// 实现父类的Write方法procedure TFather.Write;begin inherited Write; WriteLn('Father class method called.');end;

// 实现子类的Write方法procedure TMyClass.Write;begin inherited Write; WriteLn('My class method called.');end;

调用子类的`Write`方法时,会依次调用祖父类、父类和子类的方法:

delphivar MyClass: TMyClass;begin MyClass :=TMyClass.Create; MyClass.Write; MyClass.Free;end;

输出结果为:

Grand class method called.Father class method called.My class method called.

Delphi如何通过procedure of Object调用爷爷类的方法,实现跨类方法调用?

Delphi通过inherited 可以调用父类的方法,但是没有提供直接调用父类的父类的方法(爷爷类),通过变通的方式实现如下:

假设父类是TFather,爷爷类TGrand,调用爷爷类的Write方法:

1 2 3 4 5 6 7 8 9 type TWriteProc= procedure of Object ; var WriteProc:TWriteProc; .... TMethod(WriteProc).Code:[emailprotected] . Write ; TMethod(WriteProc).Data:=Self; WriteProc;

  www.cnblogs.com/hezihang/p/4139656.html

本文共计454个文字,预计阅读时间需要2分钟。

Delphi如何通过procedure of Object调用爷爷类的方法,实现跨类方法调用?

通过继承使用Delphi中的`inherited`关键字可以调用父类的方法,但无法直接调用父类的父类(即祖父类)的方法。可以通过传递的方式来间接实现。以下是一个示例:

假设我们有三个类:`TFather`、`TGrand`和`TMyClass`。

delphi// 祖父类TGrand=classpublic procedure Write; virtual;end;

// 父类TFather=class(TGrand)public procedure Write; override;end;

// 子类TMyClass=class(TFather)public procedure Write; override;end;

// 实现祖父类的Write方法procedure TGrand.Write;begin WriteLn('Grand class method called.');end;

// 实现父类的Write方法procedure TFather.Write;begin inherited Write; WriteLn('Father class method called.');end;

// 实现子类的Write方法procedure TMyClass.Write;begin inherited Write; WriteLn('My class method called.');end;

调用子类的`Write`方法时,会依次调用祖父类、父类和子类的方法:

delphivar MyClass: TMyClass;begin MyClass :=TMyClass.Create; MyClass.Write; MyClass.Free;end;

输出结果为:

Grand class method called.Father class method called.My class method called.

Delphi如何通过procedure of Object调用爷爷类的方法,实现跨类方法调用?

Delphi通过inherited 可以调用父类的方法,但是没有提供直接调用父类的父类的方法(爷爷类),通过变通的方式实现如下:

假设父类是TFather,爷爷类TGrand,调用爷爷类的Write方法:

1 2 3 4 5 6 7 8 9 type TWriteProc= procedure of Object ; var WriteProc:TWriteProc; .... TMethod(WriteProc).Code:[emailprotected] . Write ; TMethod(WriteProc).Data:=Self; WriteProc;

  www.cnblogs.com/hezihang/p/4139656.html