使用泛型的Delphi属性getter函数

问题描述 投票:2回答:1

最好有通用的属性获取器/设置器在每次访问时执行共同的任务。

该代码在Delphi XE2'E2008不兼容类型'中给出了编译时错误。类似的代码在编译过程中产生内部错误,但从不编译。我会出错还是编译器限制?

type TFoo = class
private
  function Get<T>: T;
public
  property Bar: Integer read Get<Integer>;
end;

function TFoo.Get<T>: T;
begin
  Result := 0;
end;
delphi generics properties delphi-xe2
1个回答
4
投票

以下内容在Delphi语言中可能是通用的:

  • 类,例如TFooClass<T> = class
  • 记录,例如TFooRecord<T> = record
  • 接口,例如TFooInterface<T> = interface
  • 程序类型,例如TFooProc<T> = procedure
  • 方法,例如procedure FooMethod<T>()

属性本身不能是通用的,不能使用通用的getter或setter方法实现。

© www.soinside.com 2019 - 2024. All rights reserved.