2

我正在开发一个源自商业组件套件的组件,并且遇到了我以前从未考虑过的挑战。考虑以下代码片段:

TMyClass = class
protected
  procedure SomeMethod; virtual;
end;

TMyClass1 = class(TMyClass)
protected
  procedure SomeMethod; override;
end;

TMyMode = (mmOne, mmTwo);
TMyClass2 = class(TMyClass1)
private
  FMode: TMyMode;
protected
  procedure SomeMethod; override;
public
  property Mode: TMyMode read FMode write FMode;
end;

...

procedure TMyClass2.SomeMethod;
begin
  if FMode = mmOne then inherited SomeMethod
                   else inherited TMyClass.SomeMethod;
end;

所以如果Mode = mmOne那么我照常继承,但如果是mmTwo,我仍然想继承我祖先祖先的代码,而不是祖先引入的代码。我已经尝试了上述方法,但没有成功,而且由于我以前从未遇到过这种情况,我认为这是不可能的。有接盘侠吗?

4

2 回答 2

6

您可以使用类助手执行此操作:

type
  TA = class
  public
    procedure X; virtual;
  end;

  TB = class(TA)
  public
    procedure X; override;
  end;

  TA_Helper = class helper for TA
    procedure A_X;
  end;

  TC = class(TB)
  public
    procedure X; override;
  end;

procedure TA.X;
begin
  // ...
end;

procedure TB.X;
begin
  // ...
end;

procedure TA_Helper.A_X;
begin
  inherited X; // TA.X
end;

procedure TC.X;
begin
  A_X;
  inherited X; // TB.X
end;

我认为 D2006 中存在类助手,但如果它们不存在,您也可以使用 hack 达到相同的效果:

// ...
  TA_Helper = class(TA)
    procedure A_X;
  end;
// ...
procedure TC.X;
begin
  TA_Helper(Self).A_X;
  inherited X; // TB.X
end;
于 2012-05-30T11:19:02.670 回答
4

此任务还有另一种解决方案,没有类助手或其他方法(如@hvd 答案)。self您可以获取基类方法代码地址并使用Data-pointer: updated code调用它
,无需 rtti

unit Unit4;

interface
type
    TA = class(TObject)
      protected
        procedure Test(); virtual;
    end;

    TB = class(TA)
      protected
        procedure Test(); override;
    end;

    TC = class(TB)
      public
        procedure Test(); override;
    end;

implementation

procedure TA.Test;
begin
    writeln('TA.Test()');
end;

procedure TB.Test;
begin
    writeln('TB.Test');
end;

procedure TC.Test;
var TATest : procedure of object;
begin
    writeln('TC.Test();');
    writeln('call inherited TB: ');
    inherited Test();


    writeln('call inherited TA:');
    TMethod(TATest).Data := self;
    TMethod(TATest).Code := @TA.Test;
    TATest();
end;
end.
于 2012-05-30T13:44:03.753 回答