Originale-mail to me for new edition

 

Visibility of class members

 

Every member of a class has an attribute called visibility, which is indicated by one of the reserved words private, protected, public, published, or automated. For example,

published property Color: TColor read GetColor write SetColor;

declares a published property called Color. Visibility determines where and how a member can be accessed, with private representing the least accessibility, protected representing an intermediate level of accessibility, and public, published, and automated representing the greatest accessibility.

Private, protected, and public members

Published members

Automated members

If a member’s declaration appears without its own visibility specifier, the member has the same visibility as the one that precedes it. Members at the beginning of a class declaration that don’t have a specified visibility are by default published, provided the class is compiled in the {$M+} state or is derived from a class compiled in the {$M+} state; otherwise, such members are public.

For readability, it is best to organize a class declaration by visibility, placing all the private members together, followed by all the protected members, and so forth. This way each visibility reserved word appears at most once and marks the beginning of a new section of the declaration. So a typical class declaration should like this:

type

  TMyClass = class(TControl)

  private

   ... { private declarations here}

  protected

   ... { protected declarations here }

  public

   ... { public declarations here }

  published

   ... { published declarations here }

  end;

You can increase the visibility of a member in a descendant class by redeclaring it, but you cannot decrease its visibility. For example, a protected property can be made public in a descendant, but not private. Moreover, published members cannot become public in a descendant class. For more information, see Property overrides and redeclarations.

 

Topic groups

 

See also

About class types

Classes and objects: Overview

Inheritance and scope

 

 

译文

 

类成员的可见度

 

每个类成员都由一个叫做可见度(visibility)的特征。可见度由保留字privateprotectedpublicpublishedautomated指出。例如,

published property Color: TColor read GetColor write SetColor;

这里声明了一个叫做Color的公布属性。可见度决定了类成员被访问的场所(如作用域)和方式(如读、写等特性):私有成员表示最低的可访问性,保护成员表示中间的可访问性,公共成员、公布成员和自动化成员表示最高的可访问性。

私有成员、保护成员和公共成员

公布成员

自动化成员

如果一个类成员的声明出现时没有其自身可见度的说明符,那么该成员具有与其前一个成员相同的可见度。如果类是在编译指示 {$M+} 的状态下编译或起源于在 {$M+} 状态下编译的类,那么对于该类声明中最开始的成员,如果没有指定可见度,那么省为公布的(published),否则是公共的(public)。

为了使代码具有更好的可读性,在类声明时最好按照类成员的可见度来组织,将所有的私有成员放在一起,接下来是所有的保护成员,等等。这样做使得每个可见度保留字最多出现一次并且标记了声明中新节的开始。因此,典型的类型声明应当如下:

type

  TMyClass = class(TControl)

  private

   ... { 这里是私有声明 }

  protected

   ... { 这里是保护声明 }

  public

   ... { 这里是公共声明 }

  published

   ... { 这里是公布声明 }

  end;

可以通过在后裔类中对成员再声明来提高其可见度,但不能降低其可见度。例如,保护属性可以在其后裔类中被再声明为公共属性,但不能被再声明为私有属性。此外,公布成员在其后裔类中不能成为公共成员。更多信息见属性覆盖和再声明

 

主题组

 

相关主题

关于类类型

类和对象:概述

继承和作用域