Csharp/CSharp Tutorial/Class/Access Modifier
Содержание
Access Control - demonstrate the various forms of access control
using System;
public class Class1 : Class2 {
public static void Main(string[] strings) {
Class1 class1 = new Class1();
Class2 class2 = new Class2();
Class3 class3 = new Class3();
class2.A_public();
class1.B_protected();
class1.C_private();
class3.D_internal();
class1.E_internalprotected();
class3.E_internalprotected();
}
public void C_private() {
Console.WriteLine("Class1.C_private");
}
}
internal class Class3 {
public void A_public() {
Console.WriteLine("Class3.A_public");
}
protected void B_protected() {
Console.WriteLine("Class3.B_protected");
}
internal void D_internal() {
Console.WriteLine("Class3.D_internal");
}
public void E_internalprotected() {
Console.WriteLine("Class3.E_internalprotected");
}
}
public class Class2 {
public void A_public() {
Console.WriteLine("Class2.A_public");
}
protected void B_protected() {
Console.WriteLine("Class2.B_protected");
}
private void C_private() {
Console.WriteLine("Class2.C_private");
}
internal void D_internal() {
Console.WriteLine("Class2.D_internal");
}
internal protected void E_internalprotected() {
Console.WriteLine("Class2.E_internalprotected");
}
}
C#"s Access Specifiers
An access specifier determines how other parts of a program can access a class member.
Member access is controled by four access specifiers:
- public,
- private,
- protected,
- internal.
- public member can be accessed by any other code in your program.
- Main() is declared as public because it will be called by code outside of its class (the operating system).
- private member can be accessed only by other members of its class.
- A protected member is public within a class hierarchy, but private outside that hierarchy.
- A protected member is created by using the protected access modifier.
- The internal modifier declares that a member is known throughout all files in an assembly, but unknown outside that assembly.
- The protected internal access level can be given only to class members.
- A member declared with protected internal access is accessible within its own assembly or to derived types.
7.15.Access Modifier 7.15.1. C#"s Access Specifiers 7.15.2. <A href="/Tutorial/CSharp/0140__Class/Theuseofvariousaccessmodifiers.htm">The use of various access modifiers</a> 7.15.3. <A href="/Tutorial/CSharp/0140__Class/AccessControldemonstratethevariousformsofaccesscontrol.htm">Access Control - demonstrate the various forms of access control</a> 7.15.4. <A href="/Tutorial/CSharp/0140__Class/Visibilitymodifiersindicatewhichothercodeitemscanviewanitem.htm">Visibility modifiers indicate which other code items can view an item.</a>
The use of various access modifiers
class MyClass
{
public string publicStringField;
protected internal string protectedInternalStringField;
internal string internalStringField;
protected int protectedField = 150;
private int privateField;
public void setPrivateField(int privateField)
{
this.privateField = privateField;
}
public int getPrivateField()
{
return privateField;
}
public void Start()
{
System.Console.WriteLine("Starting MyClass ...");
privateMethod();
System.Console.WriteLine("MyClass started");
}
private void privateMethod()
{
System.Console.WriteLine("Turning starter motor ...");
}
}
class MainClass
{
public static void Main()
{
MyClass myMyClass = new MyClass();
myMyClass.publicStringField = "Toyota";
myMyClass.protectedInternalStringField = "MR2";
myMyClass.internalStringField = "black";
myMyClass.setPrivateField(1995);
System.Console.WriteLine("myMyClass.publicStringField = " + myMyClass.publicStringField);
System.Console.WriteLine("myMyClass.protectedInternalStringField = " + myMyClass.protectedInternalStringField);
System.Console.WriteLine("myMyClass.internalStringField = " + myMyClass.internalStringField);
System.Console.WriteLine("myMyClass.getPrivateField() = " + myMyClass.getPrivateField());
myMyClass.Start();
}
}
myMyClass.publicStringField = Toyota myMyClass.protectedInternalStringField = MR2 myMyClass.internalStringField = black myMyClass.getPrivateField() = 1995 Starting MyClass ... Turning starter motor ... MyClass started
Visibility modifiers indicate which other code items can view an item.
Modifier Applies To Description
public Any types or members visible to any other code.
protected Any member of a type, also any nested type visible only to any derived type.
internal Any member of a type, also any nested type visible only within its containing assembly.
private Any types or members visible only inside the type to which it belongs.
protected internal Any member of a type, also any nested type visible to any code within its containing assembly and also to any code inside a derived type.