标题就差不多了...
为什么要使用构造函数
约束?
class
约束清楚地暗示了这一点。
如果你单独使用它,你就不能用你创建的东西做任何事情。
为什么它会存在?
其他信息:
请注意,在添加“构造函数”约束之前,以下代码不会编译:
program Project3;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
type
TSomeClass<T: class> = class
function GetType: T;
end;
{ TSomeClass<T> }
function TSomeClass<T>.GetType: T;
begin
Result := T.Create;
end;
begin
try
{ TODO -oUser -cConsole Main : Insert code here }
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
最佳答案
Why would you ever want to use the constructor constraint?
It's clearly implied by the class constraint.
不,不是。构造函数约束要求该类型具有公共(public)的无参数构造函数 - 然后允许调用该构造函数。
并非所有类都有公共(public)无参数构造函数。
关于delphi - Delphi 中构造函数约束的用例是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13343890/