gpt4 book ai didi

delphi - 如何访问子类中另一个属性隐藏的属性?

转载 作者:行者123 更新时间:2023-12-03 15:02:29 25 4
gpt4 key购买 nike

我正在写一个TCustomDBGrid需要访问 protected 属性 Options( TGridOptions ) 的后代组件,该属性是 TCustomGrid 的父类 ( TCustomDBGrid ) 的一部分目的。问题是在 TCustomDBGrid 类中存在一个同名的属性重新引入,但具有另一种类型 ( TDBGridOptions )。

检查简化了此声明

 TCustomGrid=  = class(TCustomControl)
protected
//I need to access this property
property Options: TGridOptions read FOptions write SetOptions
default [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine,
goRangeSelect];
end;

TCustomDBGrid = class(TCustomGrid)
protected
//a property with the same name is reintroduced in this class
property Options: TDBGridOptions read FOptions write SetOptions
default [dgEditing, dgTitles, dgIndicator, dgColumnResize, dgColLines,
dgRowLines, dgTabs, dgConfirmDelete, dgCancelOnExit, dgTitleClick, dgTitleHotTrack];
end;


TDBGridEx = class(TCustomDBGrid)
protected
//inside of this method I need to access the property TCustomGrid.Options
procedure FastDraw(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState);
end;

我弄清楚了如何使用破解器类访问此属性。

type
TCustomGridClass=class(TCustomGrid);

{ TDBGridEx }
procedure TDBGridEx.FastDraw(ACol, ARow: Integer; ARect: TRect; AState: TGridDrawState);
var
LOptions: TGridOptions;
LRect : TRect;
begin
......
LOptions := TCustomGridClass(Self).Options; //works fine
LRect := ARect;
if not (goFixedVertLine in LOptions) then
Inc(LRect.Right);
if not (goFixedHorzLine in LOptions) then
Inc(LRect.Bottom);
.....
end;

但是只是出于好奇我想知道是否存在另一种解决方法或更好的方法来解决这个问题。

最佳答案

这是使用类助手的另一种解决方法。它不像你的那么好,但很有效。

type
TCustomGridHelper = class helper for TCustomGrid
function GetGridOptions: TGridOptions;
end;

function TCustomGridHelper.GetGridOptions: TGridOptions;
begin
Result := Self.Options;
end;

procedure TDBGridEx.FastDraw(ACol, ARow: Integer; ARect: TRect;
AState: TGridDrawState);
var
LOptions: TGridOptions;
LRect : TRect;
begin
...
LOptions := Self.GetGridOptions; //works fine
...
end;

关于delphi - 如何访问子类中另一个属性隐藏的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12119112/

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