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

发布于 2024-08-06 09:15:33 字数 1554 浏览 4 评论 0原文

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

所以,我不太明白 strict private 指令和 protected 指令之间的区别。所以这是我的代码,它是关于“bag”创建的,它是刚刚介绍了我的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;

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

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 difference between the strict private instruction and the protected one.. So here is my code, it's about a "bag" creation, it's just the introduction of my Delphi's lesson, teacher show us how we can create objects:

    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;

So it's just an example of object creation; I understand what is public declaration (interface approachable by the outside) but I don't see what's the difference between private and protected declarations.. Thanks for trying to help me..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你的往事 2024-08-13 09:15:33

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

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

Delphi 中存在一个“bug”,它使同一单元内所有成员的可见性都公开。 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;

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;
ι不睡觉的鱼゛ 2024-08-13 09:15:33

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

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

protected - 与后代类中的 private PLUS 相同

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

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

叫思念不要吵 2024-08-13 09:15:33

您可以到处查找(关键字是“访问修饰符”)

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

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.

百变从容 2024-08-13 09:15:33

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

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.)

花开浅夏 2024-08-13 09:15:33

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

使用 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;

有关更多信息,请参阅此帖子:访问 delphi 类的严格保护属性

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文