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

后端 未结 5 1911
野趣味
野趣味 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: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.

提交回复
热议问题