When writing programs, we can define their access levels by using different access modifiers on the members of the class.
Public members
Public members in C # provide the external interface of the class, allowing users of the class to access them from outside. Public members are decorated with the modifier, which is the least restrictive way of access.
Private members
Private members in C # are restricted to members of the class, and access to private members from outside the class is illegal. If a member's access modifier is not present in the declaration, the member is private by default. Private member modifiers are private.
Protect members
In order to facilitate access to derived classes and to expect members to be hidden from the outside world, you can use the protected modifier to declare a member to be a protected member.
Internal members
A member of a class that uses the internal modifier is a special member. This member is transparent to the application or library in the same package, while in the package. NET is not allowed to access.
Use the following example to illustrate the use of the access modifier for a member of a class.
Program Listing 10-1:
Using System;
Class Vehicle//define the Auto class
{
public int wheels;//Publicly owned member: Wheel number
protected float weight; Protection member: Weight public
void F () {
wheels=4;//correct, allow access to its own member
weight=10;//correct, allow access to its own member
}
}
class Train//Definition train class
{
public int num;//publicly owned member: number
of carriages private int passengers;//Private Member: Number of passengers private
float weight ; Private member: Weight public
void F () {
num=5;//correct, allow access to its own member
weight=100;//correct, allow access to its own members
Vehicle v1=new Vehicle ();
v1.wheels=4; Correct, allow access to V1 public member
//v1.weight=6 error, do not allow access to V1 protection member, can be changed to:
weight=6;
}
Class Car:vehicle//Definition Sedan class
{
int passengers;//Private Member: Number of passengers public
void F () {
Vehicle v1=new Vehicle (); c30/>v1.wheels=4; Correct, allow access to V1 's public member
v1.weight=6;//correct, allow access to V1 protection member
}
}