Delphi中的静态属性

Kov*_*xey 3 delphi static properties

我遇到静态属性Calendar的问题.

type
  TDateTime = class(TObject)
  private
  class var fcalendar: TCalendar;
  class procedure SetCalendar(const Value: TCalendar);
  public
  class property Calendar: TCalendar read fcalendar write SetCalendar;
 end;

implementation

  class procedure TDateTime.SetCalendar(const Value: TCalendar);
  begin
    if Value <> nil then
    begin
      TDateTime.fcalendar := Value;
    end;
  end;
Run Code Online (Sandbox Code Playgroud)

错误发生在第7行

E2355类属性访问器必须是类字段或类静态方法

Mas*_*ler 6

问题在于设置器,错误消息准确地解释了您需要如何修复它:将其标记为静态.这确实意味着你不能使用虚拟类方法作为访问器,但是你不会这样做,所以它应该不是问题.

class procedure SetCalendar(const Value: TCalendar); static;
Run Code Online (Sandbox Code Playgroud)