C#存取修飾詞(Access Modifiers)作者:AY20080808 提交日期:2009-3-23 15:51:00 | 分類: | 訪問量:34
存取修飾詞是一些關鍵字,用於指定聲明的成員或類型的可訪問性。這些關鍵字包括 public、private、protected 和 internal。
一、可訪問性層級(Accessibility Levels)
使用這些存取修飾詞可指定下列五個可訪問性層級(Accessibility Levels):
◆public Access is not restricted.
訪問不受限制。
◆protected Access is limited to the containing class or types derived from the containing class.
存取範圍限定於它所屬的類或從該類派生的類型。
◆internal Access is limited to the current assembly.
存取範圍限定於此程式。
◆protected internal Access is limited to the current assembly or types derived from the containing class.
存取範圍限定於此程式或那些由它所屬的類派生的類型。
◆private Access is limited to the containing type.
存取範圍限定於它所屬的類型。
Only one access modifier is allowed for a member or type, except for the protected internal combination.
Access modifiers are not allowed on namespaces. Namespaces have no access restrictions.
Depending on the context in which a member declaration takes place, only certain declared accessibilities are permitted. If no access modifier is specified in a member declaration, a default accessibility is used.
Top-level types, which are not nested into other types, can only have internal or public accessibility. The default accessibility for these types is internal.
Nested types, which are members of other types, can have declared accessibilities as indicated in the following table.
----------------------------------------------------------------------
Members of | Default member | Allowed declared accessibility
| accessibility | of the member
----------------------------------------------------------------------
enum | public | None
----------------------------------------------------------------------
class | private | public
| | protected
| | internal
| | private
| | protected internal
----------------------------------------------------------------------
interface | public | None
----------------------------------------------------------------------
struct | private | public
| | internal
| | private
----------------------------------------------------------------------
The accessibility of a nested type depends on its accessibility domain, which is determined by both the declared accessibility of the member and the accessibility domain of the immediately containing type. However, the accessibility domain of a nested type cannot exceed that of the containing type.
二、成員訪問
聲明一個成員時所能選擇的可訪問性的類型,依賴於該成員聲明出現處的上下文。此外,當成員聲明不包含任何存取修飾詞時,聲明發生處的上下文會為該成員選擇一個預設的可訪問性。
◆命名空間隱式地具有 public 已聲明可訪問性。在命名空間聲明中不允許使用存取修飾詞。
編譯單元或命名空間中聲明的類型可以具有 public 或 internal 已聲明可訪問性,預設的已聲明可訪問性為 internal。
◆類成員可具有五種已聲明可訪問性中的任何一種,預設為 private 已聲明可訪問性。(請注意,聲明為類成員的類型可具有五種已聲明可訪問性中的任何一種,而聲明為命名空間成員的類型只能具有 public 或 internal 已聲明可訪問性。)
◆結構成員可以具有 public、internal 或 private 已聲明可訪問性並預設為 private 已聲明可訪問性,這是因為結構是隱式地密封的。結構的成員若是在此結構中聲明的(也就是說,不是由該結構從它的基類中繼承的)不能具有 protected 或 protected internal 已聲明可訪問性。(請注意,聲明為結構成員的類型可具有 public、internal 或 private 已聲明可訪問性,而聲明為命名空間成員的類型只能具有 public 或 internal 已聲明可訪問性。)
◆介面成員隱式地具有 public 已聲明可訪問性。在介面成員聲明中不允許使用存取修飾詞。
◆枚舉成員隱式地具有 public 已聲明可訪問性。在枚舉成員聲明中不允許使用存取修飾詞。