Restricting method access in a protected section to several classes

I want to restrict the access of protected methods to only some inherited classes.

For example, there is a base class, for example

TBase = Class
  Protected
    Method1;
    Method2;
    Method3;
    Method4;
End;

I have two classes derived from TBase

TDerived1 = Class(TBase)
  //Here i must access only Method1,Method2 and Method3
End;

TDerived2 = Class(TBase)
  //Here i must access only Method3 and Method4
End;

Then is it possible to access only Method1, Method2 and Method3 when I use TDerived1 objects and   Method3 and Method4 when I use TDerived2 objects

+5
source share
6 answers

. , . , , , .

+7

, Jeroen answer:

  TBase = class
  end;

  TBase12 = class(TBase)
  protected
    procedure Method1;
    procedure Method2;
  end;

  TBase34 = class(TBase)
  protected
    procedure Method3;
    procedure Method4;
  end;

  TDerived1 = class(TBase12)
  end;

  TDerived2 = class(TBase34)
  end;

, , , -, , "" (, Mason ).

+3

, private/protected/public, .
:

unit PropertyAndMethodVisibilityPromotionUnit;

interface

type
  TBase = class
  private
    procedure Method1;
    procedure Method2;
    procedure Method3;
    procedure Method4;
  end;

  TBase1 = class(TBase)
  protected
    procedure Method1;
    procedure Method2;
  end;

  TBase2 = class(TBase)
  protected
    procedure Method3;
    procedure Method4;
  end;

  TDerived1 = class(TBase1)
    //Here i must access only Method1 and Method2
  end;

  TDerived2 = class(TBase2)
    //Here i must access only Method3 and Method4
  end;

implementation

procedure TBase.Method1;
begin

end;

procedure TBase.Method2;
begin

end;

procedure TBase.Method3;
begin

end;

procedure TBase.Method4;
begin

end;

procedure TBase1.Method1;
begin
  inherited;
end;

procedure TBase1.Method2;
begin
  inherited;
end;

procedure TBase2.Method3;
begin
  inherited;
end;

procedure TBase2.Method4;
begin
  inherited;
end;

end.

:

  • , TBase, TBase1 TBase2 .
  • , , .

-

+1

- ...

IBase1 = interface
  // press Ctrl+Shift+G here to generate your own sexy GUID
  procedure Method1;
  procedure Method2;
end;

IBase2 = interface
  // press Ctrl+Shift+G here again
  procedure Method3;
  procedure Method4;
end;

TBase = class(TInterfacedObject, IBase1, IBase2)
public
  { IBase1 }
  procedure Method1;
  procedure Method2;
  { IBase2 }
  procedure Method3;
  procedure Method4;
end;

var
  B1: IBase1;
  B2: IBase2;
begin
  B1 := TBase.Create as IBase1;
  B2 := TBase.Create as IBase2;

  B1.Method1; // works
  B1.Method3; // Can't compile

  B2.Method3; // works
end;
+1

, .

Method1 Method2 TBase TDerived1 ... TDerived1.

Method1/2 TBase, Getter/setter TBase.

, TBase, , , .

+1

ok ... Here is a possible way to achieve what you are looking for. I think this requires Delphi 2005 or later. (Or any version that presented the visibility of "Strict Protected | private")

TBase = Class
  Strict Protected
    procedure Method1;
    procedure Method2;
    procedure Method3;
    procedure Method4;
End;

TDerived1 = Class(TBase)
  Protected
    procedure Method1;
    procedure Method2;
    procedure Method3;   
End;

TDerived2 = Class(TBase)
  Protected
    procedure Method3;
    procedure Method4;
End;

TUserClass = class
  FImplementer : TDerived1;
end;

And the methods look like this:

procedure TDerived2.Method3;
begin
  inherited Method3;
end;

But your requirements make me wonder if your method really belongs to your TBase class. It seems that they should be static procedures or maybe a class procedure of another class. I do not think that they really belong to TBase.

0
source

All Articles