Delphi – 如何“重载”对象’类型的过程
发布时间:2020-12-15 09:10:18 所属栏目:大数据 来源:网络整理
导读:‘Delphi’提供任何方法来’重载”对象’类型的过程,如 TTesting = class(TObject)PublicTypeTInformationEvent1 = procedure( x: integer ) of object; overload ;TInformationEvent1 = procedure ( x: integer ; y: string) of object; overload ;TInforma
‘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多态.这不是它. 请注意,如果您正在编写组件或控件并希望制作这些已发布的属性,则您的里程可能会有所不同表单设计师对泛型有很多支持. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |