21

但是我学习了编程,在使用 Pascal 语言进行结构化编程之后,我开始使用 Delphi 学习 OOP。

所以,我不太明白strict private指令和指令之间的区别protected。所以这是我的代码,它是关于“包”创建的,这只是我的 Delphi 课程的介绍,老师向我们展示了如何创建对象:

    uses
  SysUtils;

Type

  Tbag= class (Tobject)                                                          
    strict private                                                                
      FcontenM : single;
      Fcontent : single;
    protected
      function getisempty : boolean;
      function getisfull: boolean;
    public
      constructor creer (nbliters : single);
      procedure add     (nbliters : single);
      procedure clear   (nbliters : single);
      property contenM : single read FcontenM;
      property content : single read Fcontent;
      property isempty : boolean read getisempty;
      property isfull : boolean read getisfull;
    end;


function Tseau.getisempty;
  begin
    result := Fcontent = 0;
  end;

function Tseau.getisfull;
  begin
    result := Fcontent = FcontenM;
  end;

constructor Tseau.creer(nbliters: Single);
  begin
    inherited create;
    FcontenM := nbliters;
  end;

procedure Tbag.add (nbliters: Single);
  begin
    if ((FcontenM - Fcontent) < nbliters) then fcontent := fcontenM
      else Fcontent := (Fcontent + nbliters);
  end;

procedure Tbag.clear (nbliters: Single);
  begin
    if (Fcontent > nbliters) then Fcontent := (Fcontent - nbliters)
      else Fcontent := 0;
  end;

所以这只是对象创建的一个例子;我了解什么是公共声明(外部可访问的界面),但我看不出私有声明和受保护声明之间有什么区别。感谢您尝试帮助我。

4

5 回答 5

35

私有、受保护和公共之间的区别非常简单:

  • 私有成员/方法仅在声明它们的类中可见。
  • 受保护的成员/方法在类中是可见的,并且对所有子类都是可见的。
  • 公共成员和方法对所有其他类可见。

在 Delphi 中有一个“错误”,它使所有成员的可见性在同一单元内公开。strict关键字纠正了这种行为,因此 private 实际上是私有的,即使在单个单元中也是如此。为了获得良好的封装,我建议始终使用 strict 关键字。

示例代码:

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;
于 2009-10-04T14:35:50.560 回答
6

严格私有 - 仅在此类中可见和可访问。

private - 只能从这个类和这个类单元中可见和访问。

protected - 与后代类中的私有 PLUS 相同

您可以在此处阅读有关封装的更多信息和想法:http ://en.wikipedia.org/wiki/Encapsulation_%28computer_science%29#Encapsulation

于 2009-10-04T14:15:49.743 回答
5

您可以在任何地方查找它(关键字是“访问修饰符”)

基本上,受保护意味着成员将在子类和整个单元中可见。严格私有意味着您只能访问此类的成员方法中的成员。

于 2009-10-04T14:11:51.913 回答
5

其他答案中缺少一个案例:private甚至strict private 可以从其类中的代码访问其他实例的字段

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;

(这与 Java 中的行为相同。)

于 2011-12-05T09:09:41.490 回答
3

其他答案中缺少另一种情况。有可能“扩展”类封装规则。

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.

于 2011-12-06T23:41:06.823 回答