如何访问为子类中的其他属性隐藏的属性?

时间:2012-08-25 04:17:33

标签: delphi oop

我正在编写一个TCustomDBGrid后代组件,需要访问受保护属性Options(TGridOptions),它是{{3的父类(TCustomGrid)的一部分对象。问题是在TCustomDBGrid类中存在具有相同名称​​重新引入但具有其他类型(TCustomDBGrid)的属性。

检查简化此声明

 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;

我想出了如何使用cracker类访问此属性。

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;

仅仅是为了好奇我想知道是否存在另一种解决方法或更好的解决方法。

1 个答案:

答案 0 :(得分:1)

以下是使用class helpers的另一种解决方法。它并不像你的那么好,但有效。

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;