HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Package is defined as "to one or more items enclosed in a physical or logical package."In object-oriented programming methodology, the package is to prevent access to implementation details.
Abstraction and encapsulation are related to object-oriented programming features. Abstraction allows visualization of relevant information, the package allows programmersto achieve the desired level of abstraction.
Packageaccess modifiers to achieve.Anaccess modifier defines the scope and visibility of a class member.C # supports access modifier as follows:
Public access modifier allows a class to its member variables and member functions exposed to other functions and objects. Any member of the public can be accessed outside the class.
The following example illustrates this point:
using System; namespace RectangleApplication { class Rectangle { // Member variables public double length; public double width; public double GetArea () { return length * width; } public void Display () { Console.WriteLine ( "Length: {0}", length); Console.WriteLine ( "width: {0}", width); Console.WriteLine ( "Size: {0}", GetArea ()); } } // Rectangle class ExecuteRectangle end { static void Main (string [] args) { Rectangle r = new Rectangle (); r.length = 4.5; r.width = 3.5; r.Display (); Console.ReadLine (); } } }
When the above code is compiled and executed, it produces the following results:
长度: 4.5 宽度: 3.5 面积: 15.75
In the example above, the member variables length and width are declared aspublic, so they can be function Main () Example rvisit Rectangle class.
Member functionDisplay ()andGetArea ()you can access these variables directly.
Member functionDisplay ()has also been declared aspublic, so it can also beMain ()Example rvisit Rectangle class.
Private access modifier allows a class to its member variables and member functions to other functions and objects hidden. Only in the same class of functions you can access its private members. Even the class instance can not access its private members.
The following example illustrates this point:
using System; namespace RectangleApplication { class Rectangle { // Member variables private double length; private double width; public void Acceptdetails () { Console.WriteLine ( "Please enter the length:"); length = Convert.ToDouble (Console.ReadLine ()); Console.WriteLine ( "Please enter the width:"); width = Convert.ToDouble (Console.ReadLine ()); } public double GetArea () { return length * width; } public void Display () { Console.WriteLine ( "Length: {0}", length); Console.WriteLine ( "width: {0}", width); Console.WriteLine ( "Size: {0}", GetArea ()); } } // End class Rectangle class ExecuteRectangle { static void Main (string [] args) { Rectangle r = new Rectangle (); r.Acceptdetails (); r.Display (); Console.ReadLine (); } } }
When the above code is compiled and executed, it produces the following results:
Please enter the length: 4.4 Please enter the width: 3.3 Length: 4.4 Width: 3.3 Area: 14.52
In the example above, the member variables length and width are declared asprivate, so they can not be the function Main () access.
Member functionAcceptDetails ()andDisplay ()you can access these variables.
Since the member functionAcceptDetails ()andDisplay ()is declared aspublic, so they can beMain ()Example rvisit Rectangle class.
Protected access modifiers allow subclasses access to its member variables and member functions of the base class. This helps achieve inheritance. We will discuss this in more detail in the section inheritance. We discuss this in more detail.
Internal access specifier allows a class to its member variables and member functions exposed to the current program other functions and objects. In other words, any member with internal access modifier can be defined in any class or method of the member as defined by the application access.
The following example illustrates this point:
using System; namespace RectangleApplication { class Rectangle { // Member variable internal double length; internal double width; double GetArea () { return length * width; } public void Display () { Console.WriteLine ( "Length: {0}", length); Console.WriteLine ( "width: {0}", width); Console.WriteLine ( "Size: {0}", GetArea ()); } } // End class Rectangle class ExecuteRectangle { static void Main (string [] args) { Rectangle r = new Rectangle (); r.length = 4.5; r.width = 3.5; r.Display (); Console.ReadLine (); } } }
When the above code is compiled and executed, it produces the following results:
Length: 4.5 Width: 3.5 Area: 15.75
In the example above, note that the member functionGetArea ()when the statement without any access modifiers. If you do not specify an access modifier, use class members default access modifier isprivate.
Protected Internal access modifier allows a class to its member variables and member functions of other classes of objects and the subclass within the same application other than to hide. This is also used for inheritance.