我有一个基类和10个派生自该类的类。基类包含除procedure of object
类型的参数之外的函数。像这样
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;
我在派生类中重载此函数,即派生类包含相同的函数但具有不同的参数(对象的过程(const handler:integer))。像这样
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;
依旧.........
我想要一个实现此功能的泛型类,并从该函数派生我的类,例如
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;
由于泛型约束不支持类型procedure of object
答案 0 :(得分:3)
您需要一个具有内部类型定义的泛型类:
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;