“严格私人”和“受保护”访问修饰符在Delphi? [英] Difference between "strict private" and "protected" Access Modifiers in Delphi?
问题描述
但是我用Pascal语言学习编程和结构化编程之后,我开始用Delphi了解OOP。
but I learn programming and after structured programming with Pascal language, I'm beginning to learn about OOP with Delphi.
所以,我真的不明白 strict private
指令和受保护的
一个..所以这里是我的代码,它是关于一个包创建时,只是介绍了我的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..
推荐答案
私人,受保护和公开之间的区别非常简单:
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.
在Delphi中有一个错误,使所有成员在同一个单位内公开的可见性。 严格关键字可以纠正此问题,因此即使在单个单元中,私有功能也是私有的。对于良好的封装,我建议始终使用strict关键字。
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.
示例代码:
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;
这篇关于“严格私人”和“受保护”访问修饰符在Delphi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!