【发布时间】:2009-07-21 13:24:23
【问题描述】:
首先,对这篇文章的长度表示歉意。如果简洁是智慧的灵魂,那么这是一个愚蠢的问题。
我认为我的问题归结为:
在 Delphi 子类中覆盖常量数组的最佳方法是什么?
背景:
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -=-=-=-=-=-=-
我有一个在父类和许多子类中定义的常量数组。 数组元素的类型始终相同,但元素的数量和确切数据因一个子元素而异(我正在描述数据库表,因为特定的网格控件在编译时需要元数据,但这不是重点)。
我有几个作用于这些数组的函数。作为一个简单的例子,我可能有一个函数来返回数组的最后一个元素。
如果您在父级中定义“GetLastElement”,然后调用从子级继承的函数,它仍将作用于 parents 版本的数组。这不是我所期望的。看起来孩子们应该在他们自己的本地版本的数组上调用继承的函数。
目前我必须在每个子类中复制这些函数,这令人抓狂。
我希望能够对我的常量数组的本地版本进行继承函数。最好的方法是什么?我考虑过在返回静态数组的基类中定义一个函数,然后为每个孩子覆盖它。如果我这样做了,那么我就不会对数组进行操作,而是对函数结果进行操作。
这将解决继承问题,但它在 I'd have to define a new type to encapsulate the array 中引入了一个新问题,并修改了我的(已经很复杂的)网格控件以使用该类型。
欢迎提出任何建议。
下面是一个简化的应用程序,演示了我在说什么:
主要形式:
implementation
{$R *.dfm}
uses
ParentClass, Descendant1, Descendant2;
procedure TfrmMain.btnTestClick(Sender: TObject);
var
d1, d2: TParentClass;
begin
d1 := TDescendant1.Create;
d2 := TDescendant2.Create;
//as it stands, this will return "E", then "A", which is good.
//but if you move the LastElementOfArray function to the ParentClass,
//then it will return "E", "E", ignoring the version of the array
//defined inside TDescendant2.
ShowMessage('d1=' + d1.LastElementOfArray);
ShowMessage('d2=' + d2.LastElementOfArray);
end;
end.
在名为 ParentClass.pas 的文件中:
unit ParentClass;
interface
type
TParentClass = class
public
function LastElementOfArray: string; virtual; abstract;
end;
const
c_MyConstantArray: array[1..5] of string = ('A','B','C','D','E');
implementation
end.
在一个名为 Descendant1.pas 的单元中
//here, we will just take whatever array we got from the parent
unit Descendant1;
interface
uses
ParentClass;
type
TDescendant1 = class(TParentClass)
public
function LastElementOfArray: string; override;
end;
implementation
{ TDescendant1 }
function TDescendant1.LastElementOfArray: string;
begin
Result := c_MyConstantArray[High(c_MyConstantArray)];
end;
end.
在名为 Descendant2.pas 的文件中
//override with a new version of the constant array (same length)
unit Descendant2;
interface
uses
ParentClass;
type
TDescendant2 = class(TParentClass)
public
function LastElementOfArray: string; override;
end;
const
c_MyConstantArray: array[1..5] of string = ('E','D','C','B','A');
implementation
{ TDescendant2 }
function TDescendant2.LastElementOfArray: string;
begin
//I hate defining this locally, but if I move it to ParentClass,
//then it will act on the ParentClass version of the array, which
//is **NOT** what I want
Result := c_MyConstantArray[High(c_MyConstantArray)];
end;
end.
【问题讨论】:
-
您的常量
C_MyConstantArray肯定是在类定义之外定义的,因此是全局(单位范围)常量。大概是 ParentClass 版本正在被使用,因为它首先出现在主窗体的 uses 子句中,尽管这不是我直观地期望的。
标签: delphi inheritance