gpt4 book ai didi

delphi - 如何在Delphi中重写继承的类属性?

转载 作者:行者123 更新时间:2023-12-03 15:27:00 24 4
gpt4 key购买 nike

在一个文件中,我有一个带有 ID 属性的基类:

type
TBase = class
private
class function GetID(ACombo: TCombo): Integer; virtual;
class procedure SetID(ACombo: TCombo; AValue: Integer); virtual;
public
class property ID[ACombo: TCombo]: Integer read GetID write SetID;
end;

在第二个文件中,我有另一个类,来自 TBase。由于意外、无知或其他原因,创建了一个与现有属性/字段同名的新属性/字段。

type
TSubBase = class(TBase)
private
class function GetID(ACombo: TCombo): Integer; override;
class procedure SetID(ACombo: TCombo; AValue: Integer); override;
end;

并以如下方式使用这些类:

  TBaseClass = class of TBase;

function Base(): TBaseClass;

implementation

var
BaseInstance: TBaseClass;

function Base(): TBaseClass;
begin
if not Assigned(BaseInstance) then
begin
if SOME_PARAM then
BaseInstance:= TBase
else
BaseInstance:= TSubBase;
end;
Result := BaseInstance;
end;
<小时/>
if Base.StationCode[cmbStation] = SOME_VALUE then

但是我在编译时遇到错误:

[DCC Error] uMyFile.pas(69): E2355 Class property accessor must be a class field or class static method
<小时/><小时/>

我也一直在尝试使用静态关键字...并且根据下面同事的建议找到了一些解决方法。

type
TBase = class
private
class function GetIDStatic(ACombo: TCombo): Integer; static;
class procedure SetIDStatic(ACombo: TCombo; AValue: Integer); static;
class function GetID(ACombo: TCombo): Integer; virtual; abstract;
class procedure SetID(ACombo: TCombo; AValue: Integer); virtual; abstract;
public
class property ID[ACombo: TCombo]: Integer read GetIDStatic write SetIDStatic;
end;

TSubBase = class(TBase)
private
class function GetID(ACombo: TCombo): Integer; override;
class procedure SetID(ACombo: TCombo; AValue: Integer); override;
end;

TBaseClass = class of TBase;

function Base(): TBaseClass;

implementation

var
BaseInstance: TBaseClass;

function Base(): TBaseClass;
begin
if not Assigned(BaseInstance) then
begin
if SOME_PARAM then
BaseInstance:= TBase
else
BaseInstance:= TSubBase;
end;
Result := BaseInstance;
end;

class function TBase.GetIDStatic(ACombo: TCombo): Integer; static;
begin
Result := BaseInstance.GetID(ACombo);
// Or maybe below ?
// Result := Base().GetID(ACombo);
end;

class procedure TBase.SetIDStatic(ACombo: TCombo; AValue: Integer); static;
begin
BaseInstance.SetID(ACombo, AValue);
// Or maybe below ?
// Base().SetID(ACombo, AValue);
end;

但是在最后一个变体中 - 实现很丑陋,我同意 David 关于使用类属性的方法留下“梦想”并简单地重构,如下所述:

  class properties ID[ACombo: TCombo]: Integer ....
=>>
class function GetID(ACombo: TCombo): Integer; virtual;
class pocedure SetID(ACombo: TCombo; AValue: Integer); virtual;

感谢大家在那里进行有趣的挖掘!

最佳答案

错误本身告诉我们:“类属性访问器必须是...类静态方法”

您告诉我们可以编译 TBase,但在添加 TSubBase 时出现错误。但是TBase 不应该被允许编译。如果是 - 那么 Delphi 中存在错误。

http://docwiki.embarcadero.com/RADStudio/XE5/en/Methods#Class_Methods

In the defining declaration of a class method, the identifier Self represents the class where the method is called (which can be a descendant of the class in which it is defined.) If the method is called in the class C, then Self is of the type class of C. Thus you cannot use Self to access instance fields, instance properties, and normal (object) methods. You can use Self to call constructors and other class methods, or to access class properties and class fields.

因此,我们可以考虑一些解决方法,明确声明我们想要 cqall 方法的类。类似这样的事情:

class function GetIDStatic(ACombo: TCombo): Integer; static;
var RealClass: TBaseClass;
begin
RealClass := Self; /// will not compile !!!
Result := RealClass.GetID(ACombo);
end;

但是...

Unlike ordinary class methods, class static methods have no Self parameter at all

因此静态方法无法知道在调用站点调用了哪个类。因此,他们准确地调用了在自己的类中定义的函数体。在这个地方 - 这将完全是一个抽象函数。

但是有一个明显的解决方法可以允许编写类似的内容

if Base().n.StationCode[cmbStation] = SOME_VALUE then

但是实现效率很低,因此不值得。

关于delphi - 如何在Delphi中重写继承的类属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20100827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com