Delphi中“严格私有”和“受保护”访问修饰符之间的区别?

时间:2009-10-04 14:06:46

标签: delphi access-modifiers

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

所以,我并不真正理解strict private指令和protected指令之间的区别。所以这是我的代码,它是关于“包”的创建,它只是引入我的德尔菲课程,老师告诉我们如何创建对象:

    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;

所以它只是对象创建的一个例子;我理解什么是公开声明(界面易于接受)但我不知道私有声明和受保护声明之间有什么区别..感谢您试图帮助我..

5 个答案:

答案 0 :(得分:32)

私人,受保护和公开之间的区别非常简单:

  • 私有成员/方法仅在声明它们的类中可见。
  • 受保护的成员/方法在类中可见,显示在所有子类中。
  • 所有其他类都可以看到公共成员和方法。

在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;

答案 1 :(得分:6)

严格私密 - 仅在此课程中可见和可访问。

私有 - 仅在此类和此类单元中可见和可访问。

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

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

答案 2 :(得分:5)

你可以在任何地方看到这个(关键字是“访问修饰符”)

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

答案 3 :(得分:4)

其他答案中缺少一个案例:其他实例的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中的行为相同。)

答案 4 :(得分:1)

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

使用Delphi 8中引入的类助手(用于.NET兼容性),可以规避 私人,受保护和公共(甚至是严格的注释)之间的可见性差异。 类助手声明可以在原始类之外的其他单元中。

这是一个例子:

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;

有关详情,请参阅此帖子:access-a-strict-protected-property-of-a-delphi-class

相关问题