Difference between “strict private” and “protected” Access Modifiers in Delphi?

后端 未结 5 1907
野趣味
野趣味 2020-12-31 03:23

but I learn programming and after structured programming with Pascal language, I\'m beginning to learn about OOP with Delphi.

So, I don\'t really understand the diff

相关标签:
5条回答
  • 2020-12-31 03:46

    One case is missing in the other answers: private and even strict private fields of other instances can be accessed from code within their class:

    type
      TSO1516493= class
      strict private
        A: Integer;
      public
        procedure ChangeOther(Param: TSO1516493);
      end;
    
    { TSO1516493 }
    
    procedure TSO1516493.ChangeOther(Param: TSO1516493);
    begin
      Param.A := -1; // accessing a strict private variable in other instance !
    end;
    

    (This is the same behavior as in Java.)

    0 讨论(0)
  • 2020-12-31 03:50

    The difference between private, protected and public is pretty straightforward:

    • Private members/methods are only visible within the class that declares them.
    • Protected members/methods are visible within the class, and to all subclasses.
    • Public members and methods are visible to all other classes.

    In Delphi there's a "bug" that makes the visibility of all members public within the same unit. The strict keyword corrects this behaviour, so that private is actually private, even within a single unit. For good encapsulation I would recommend always using the strict keyword.

    Example code:

    type
      TFather = class
      private
        FPriv : integer;
      strict private
        FStrPriv : integer;
      protected
        FProt : integer;
      strict protected
        FStrProt : integer;
      public
        FPublic : integer;
      end;
    
      TSon = class(TFather)
      public
        procedure DoStuff;
      end;
    
      TUnrelated = class
      public
        procedure DoStuff;
      end;
    
    procedure TSon.DoStuff;
    begin
      FProt := 10;       // Legal, as it should be. Accessible to descendants.
      FPriv := 100;      // Legal, even though private. This won't work from another unit!
      FStrictPriv := 10; // <- Compiler Error, FStrictPrivFather is private to TFather
      FPublic := 100;    // Legal, naturally. Public members are accessible from everywhere.
    end;
    
    procedure TUnrelated.DoStuff;
    var
      F : TFather;
    begin
      F := TFather.Create;
      try
        F.FProt := 10;     // Legal, but it shouldn't be!
        F.FStrProt := 100; // <- Compiler error, the strict keyword has "made the protection work"
        F.FPublic := 100;  // Legal, naturally.
      finally
        F.Free;
      end;
    end;
    
    0 讨论(0)
  • 2020-12-31 03:52

    One other case missing in the other answers. There are possibilities to "extend" the class encapsulation rules.

    With class helpers, introduced in Delphi 8 (for .NET compatibility), it is possible to circumvent the difference in visibility between private,protected and public (and even the strict notations). The class helper declaration can be in another unit than the original class.

    This is an example :

    type
      TMyOrgClass = class
      strict private
        FMyPrivateProp: Integer;
      strict protected
        property MyPrivateProp: Integer read FMyPrivateProp;
      end;
    
      TMyClassHelper = class helper for TMyOrgClass
      private
        function GetMyPublicProp: Integer;
      public
        property MyPublicProp: Integer read GetMyPublicProp;
      end;
    
    function TMyClassHelper.GetMyPublicProp: Integer;
    begin
      Result:= Self.FMyPrivateProp;  // Access the org class members with Self
    end;
    

    See this post for more information :access-a-strict-protected-property-of-a-delphi-class.

    0 讨论(0)
  • 2020-12-31 03:58

    You could have looked this up everywhere (the keyword would be "access modifiers")

    Basically, protected means that the members will be visible in child classes and throughout the unit. strict private means you have access to the member in member methods of this class ONLY.

    0 讨论(0)
  • 2020-12-31 04:02

    strict private - visible and accesible only from within this class.

    private - visible and accesible only from within this class AND this class unit.

    protected - the same as private PLUS from within descendant classes

    You can read more about and idea of encapsulation here: http://en.wikipedia.org/wiki/Encapsulation_%28computer_science%29#Encapsulation

    0 讨论(0)
提交回复
热议问题