[英]how to do Generic like Implementation of class function in delphi
i have a base class and 10 class derived from that class. 我有一个基类和从该类派生的10个类。 Base class contain a function that except parameter of type
procedure of object
. 基类包含一个函数,该函数除了
procedure of object
类型procedure of object
参数外。 like this 像这样
mytype = procedure (a : integer) of object;
baseclass = class
public
procedure myproc(cont handler : mytype ) ;
end;
procedure baseclass.myproc(cont handler : mytype ) ;
begin
// do something
end;
i am overloading this function in derived class ie derived class contain same function but with different parameter (procedure ( const handler : integer ) of object ). 我在派生类中重载了该函数,即派生类包含相同的函数,但具有不同的参数(对象的过程(const handler:integer))。 like this
像这样
base1mytype = procedure (a : string) of object;
derivedclass1 = class(baseclass)
public
procedure myproc(cont handler : base1mytype ) ;overload;
end;
base2mytype = procedure (a : boolean) of object;
derivedclass1 = class(baseclass)
public
procedure myproc(cont handler : base2mytype ) ;overload;
end;
and so on......... 等等.........
All i want a generic class that implement this function and i derive my classes from that function eg 我只想要一个实现该功能的通用类,然后从该功能派生我的类,例如
mytype = procedure (a : integer) of object;
baseclass<T> = class
public
procedure myproc(cont handler : T) ;
end;
procedure baseclass<T>.myproc(cont handler : T ) ;
begin
// do something
end;
and derive classes are like this
deriveclass1 = class<baseclass <string>>
public
procedure myproc(cont handler : T) ;
end;
Since generic constraint does not support constrain of type procedure of object
由于泛型约束不支持
procedure of object
类型procedure of object
约束
You need a generic class with an internal type definition: 您需要一个具有内部类型定义的泛型类:
type
TBaseClass<T> = class
public
type
THandler = procedure(Arg: T) of object;
public
procedure CallHandler(Handler: THandler; Arg: T);
end;
procedure TBaseClass<T>.CallHandler(Handler: THandler; Arg: T);
begin
Handler(Arg);
end;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.