Delphi中如何实现对象类型的重载过程?

更新于
2026-10-04 23:49:07
1阅读来源:SEO教程
  • 内容介绍
  • 相关推荐

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

Delphi 提供多种方法来重载对象类型,以下是一个示例:

delphiclass TTesting=class(TObject)public type TInformationEvent=procedure (x: integer) of object; TInformationEvent2=procedure (x: integer; y: string) of object;

procedure InformationEvent1(x: integer); overload; procedure InformationEvent1(x: integer; y: string); overload;end;

‘Delphi’提供任何方法来’重载”对象’类型的过程,如

TTesting = class(TObject) Public Type TInformationEvent1 = procedure( x: integer ) of object; overload ; TInformationEvent1 = procedure ( x: integer ; y: string) of object; overload ; TInformationEvent1 = procedure ( x: integer ; y: string; z: Boolean) of object; overload ; end

我可以用这三种方式重载这个TInformationEvent1函数吗?

好吧.您可以定义具有相同名称但不同数量的类型参数的泛型类型.

type TInformationEvent<T> = procedure(x:T) of object; TInformationEvent<T1,T2> = procedure(x:T1;y:T2) of object; TInformationEvent<T1,T2,T3> = procedure(x:T1; y:T2; z:T3) of object;

然后,当您将其中一个作为类的成员添加时,您需要解析类型参数.

type TMyClass = class private FMyEvent: TInformationEvent<Integer>; FMyEvent2: TInformationEvent<Integer,string>; public property MyEvent: TInformationEvent<Integer> read FMyEvent write FMyEvent; property MyEvent2: TInformationEvent<Integer,string> read FMyEvent2 write FMyEvent2; end;

就编译器而言,这些在技术上是不同的命名类型,但从开发人员的角度来看,您不需要为每种类型提供唯一的名称.请注意,不需要使用overload关键字,实际上是在定义过程类型时使用的语法错误.重载具有非常特定的含义:ad hoc多态.这不是它.

请注意,如果您正在编写组件或控件并希望制作这些已发布的属性,则您的里程可能会有所不同表单设计师对泛型有很多支持.

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

Delphi 提供多种方法来重载对象类型,以下是一个示例:

delphiclass TTesting=class(TObject)public type TInformationEvent=procedure (x: integer) of object; TInformationEvent2=procedure (x: integer; y: string) of object;

procedure InformationEvent1(x: integer); overload; procedure InformationEvent1(x: integer; y: string); overload;end;

‘Delphi’提供任何方法来’重载”对象’类型的过程,如

TTesting = class(TObject) Public Type TInformationEvent1 = procedure( x: integer ) of object; overload ; TInformationEvent1 = procedure ( x: integer ; y: string) of object; overload ; TInformationEvent1 = procedure ( x: integer ; y: string; z: Boolean) of object; overload ; end

我可以用这三种方式重载这个TInformationEvent1函数吗?

好吧.您可以定义具有相同名称但不同数量的类型参数的泛型类型.

type TInformationEvent<T> = procedure(x:T) of object; TInformationEvent<T1,T2> = procedure(x:T1;y:T2) of object; TInformationEvent<T1,T2,T3> = procedure(x:T1; y:T2; z:T3) of object;

然后,当您将其中一个作为类的成员添加时,您需要解析类型参数.

type TMyClass = class private FMyEvent: TInformationEvent<Integer>; FMyEvent2: TInformationEvent<Integer,string>; public property MyEvent: TInformationEvent<Integer> read FMyEvent write FMyEvent; property MyEvent2: TInformationEvent<Integer,string> read FMyEvent2 write FMyEvent2; end;

就编译器而言,这些在技术上是不同的命名类型,但从开发人员的角度来看,您不需要为每种类型提供唯一的名称.请注意,不需要使用overload关键字,实际上是在定义过程类型时使用的语法错误.重载具有非常特定的含义:ad hoc多态.这不是它.

请注意,如果您正在编写组件或控件并希望制作这些已发布的属性,则您的里程可能会有所不同表单设计师对泛型有很多支持.