Delphi中如何通过重载对象类型的过程实现长尾功能?
- 内容介绍
- 相关推荐
本文共计538个文字,预计阅读时间需要3分钟。
Delphi 提供了多种方法来重载对象类型的过程。以下是一个简化的示例,展示了如何重载 `TInformationEvent1` 类型的过程:
delphiclass TTesting public Type TInformationEvent1=procedure (x: integer) of object; TInformationEvent2=procedure (x: integer; y: string) of object;
procedure InformationEvent1(x: integer); procedure InformationEvent1(x: integer; y: string);end;
在这个例子中,`TTesting` 类定义了两个重载的 `InformationEvent1` 过程。第一个过程只接受一个 `integer` 参数,而第二个过程接受两个参数:一个 `integer` 和一个 `string`。这样,同一个过程名可以用于不同的参数类型,增强了代码的灵活性和可读性。
‘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多态.这不是它.
请注意,如果您正在编写组件或控件并希望制作这些已发布的属性,则您的里程可能会有所不同表单设计师对泛型有很多支持.
本文共计538个文字,预计阅读时间需要3分钟。
Delphi 提供了多种方法来重载对象类型的过程。以下是一个简化的示例,展示了如何重载 `TInformationEvent1` 类型的过程:
delphiclass TTesting public Type TInformationEvent1=procedure (x: integer) of object; TInformationEvent2=procedure (x: integer; y: string) of object;
procedure InformationEvent1(x: integer); procedure InformationEvent1(x: integer; y: string);end;
在这个例子中,`TTesting` 类定义了两个重载的 `InformationEvent1` 过程。第一个过程只接受一个 `integer` 参数,而第二个过程接受两个参数:一个 `integer` 和一个 `string`。这样,同一个过程名可以用于不同的参数类型,增强了代码的灵活性和可读性。
‘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多态.这不是它.
请注意,如果您正在编写组件或控件并希望制作这些已发布的属性,则您的里程可能会有所不同表单设计师对泛型有很多支持.

