1. Trang chủ
  2. » Thể loại khác

OReilly C Sharp Language Pocket Reference Nov 2002 ISBN 059600429X

3 33 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Nội dung

1.11 Access Modifiers To promote encapsulation, a type or type member may hide itself from other types or other assemblies by adding one of the following five access modifiers to the declaration: public The type or type member is fully accessible This is the implicit accessibility for enum members (see the later section Section 1.14) and interface members (see the later section Section 1.13) internal The type or type member in assembly A is accessible only from within A This is the default accessibility for nonnested types, and so it may be omitted private The type member in type T is accessible only from within T This is the default accessibility for class and struct members, and so it may be omitted protected The type member in class C is accessible from within C or from within a class that derives from C protected internal The type member in class C and assembly A is accessible from within C, from within a class that derives from C, or from within A Note that C# has no concept of protected and internal, whereby "a type member in class C and assembly A is accessible only from within C, or from within a class that both derives from C and is within A." Note that a type member may be a nested type Here is an example of using access modifiers: // Assembly1.dll using System; public class A { private int x=5; public void Foo( ) {Console.WriteLine (x);} protected static void Goo( ) {} protected internal class NestedType {} } internal class B { private void Hoo ( ) { A a1 = new A ( ); // ok Console.WriteLine(a1.x); // error, A.x is private A.NestedType n; // ok, A.NestedType is internal A.Goo( ); // error, A's Goo is protected } } // Assembly2.exe (references Assembly1.dll) using System; class C : A { // C defaults to internal static void Main( ) { // Main defaults to private A a1 = new A( ); // ok a1.Foo( ); // ok C.Goo( ); // ok, inherits A's protected static memb new A.NestedType( ); // ok, A.NestedType is protect new B( ); // error, Assembly 1's B is internal Console.WriteLine(x); // error, A's x is private } } 1.11.1 Restrictions on Access Modifiers A type or type member cannot declare itself to be more accessible than any of the types it uses in the declaration For instance, a class cannot be public if it derives from an internal class, or a method cannot be protected if the type of one of its parameters is internal to the assembly The rationale behind this restriction is that whatever is accessible to another type is actually usable by that type In addition, access modifiers cannot be used when they conflict with the purpose of inheritance modifiers For example, a virtual (or abstract) member cannot be declared private, since it would be impossible to override Similarly, a sealed class cannot define new protected members, since there is no class that could benefit from this accessibility Finally, to maintain the contract of a base class, a function member with the override modifier must have the same accessibility as the virtual member it overrides ... 1.11.1 Restrictions on Access Modifiers A type or type member cannot declare itself to be more accessible than any of the types it uses in the declaration For instance, a class cannot be public if it... Here is an example of using access modifiers: // Assembly1.dll using System; public class A { private int x=5; public void Foo( ) {Console.WriteLine (x);} protected static void Goo( ) {} protected internal class NestedType {}... is that whatever is accessible to another type is actually usable by that type In addition, access modifiers cannot be used when they conflict with the purpose of inheritance modifiers For example, a virtual (or abstract) member cannot be declared private, since it would be impossible to override

Ngày đăng: 26/03/2019, 17:15