Delphi泛型中的多态性
发布时间:2020-12-15 09:26:57 所属栏目:大数据 来源:网络整理
导读:type TParent=class public member1:Integer; end; TChild=class(TParent) public member2:Integer; end; TGArrayT: TParent=class public function test:T; end; implementation var g:TGArrayTChild; function TGArrayT.test:T; begin Result:=??.create;
type TParent=class public member1:Integer; end; TChild=class(TParent) public member2:Integer; end; TGArray<T: TParent>=class public function test:T; end; implementation var g:TGArray<TChild>; function TGArray<T>.test:T; begin Result:=??.create; // <<<< Problem ! end; begin g := TGArray<TChild>.Create; g.test.member2 := 1234; end. g.test必须返回该类的实例.我尝试过多种方法: 1. Result := Result.create; //Exception 2. Result := TChildClass.Create; //Error 3. type TGArray<T: class> = class; //and above 2. The same errors/exceptions. 这样做的目的是创建一个通用的类数组.数组存储在泛型类中并返回实例,但是如何? 如果我完成这件事,我会缩短我的代码3次,但我不能这样做.请建议任何解决方案. 解决方法
你没有说出#2中的错误是什么,但我敢打赌它告诉你它
requires a constructor constraint.添加一个它应该有效.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |