C # Tutorial C # Encapsulation

Source: Internet
Author: User
Tags modifiers

C # Encapsulation

Encapsulation is defined as "enclosing one or more items in a physical or logical package". In the object-oriented programming methodology, encapsulation is designed to prevent access to the implementation details.

Abstraction and encapsulation are the related features of object-oriented programming. Abstraction allows for the visualization of related information, and encapsulation enables the programmer to achieve the desired level of abstraction.

Encapsulation is implemented by using access modifiers. An access modifier defines the scope and visibility of a class member. The access modifiers supported by C # are as follows:

Public

Private

Protected

Internal

Protected Internal

Public access modifier

The public access modifier allows a class to expose its member variables and member functions to other functions and objects. Any public member can be accessed by an external class.

This is illustrated in the following example:

Using System;namespace rectangleapplication{    class Rectangle    {        ///Member variable 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 ("Area: {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 above example, the member variables length and width are declared as public, so they can be accessed by the function Main () Using instance R of the Rectangle class.

The member function Display () and Getarea () can also access these variables without accessing them directly from an instance of the class.

The member function Display () is also declared public, so it can also be accessed by Main () using instance R of the Rectangle class.

Private access modifier

The Private access modifier allows a class to hide its member variables and member functions from other functions and objects. Only a function in the same class can access its private members. Even an instance of a class cannot access its private members.

This is illustrated in the following example:

using System;namespace rectangleapplication{class Rectangle {//member variable        private double length;        private double width;            public void Acceptdetails () {Console.WriteLine ("Please enter length:");            Length = Convert.todouble (Console.ReadLine ());            Console.WriteLine ("Please enter 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 ("Area: {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 input length: 4.4 Please enter width: 3.3 Length: 4.4 Width: 3.3 Area: 14.52

In the above example, the member variable length and width are declared private, so they cannot be accessed by the function Main ().

The member functions Acceptdetails () and Display () can access these variables.

Because member functions Acceptdetails () and Display () are declared public, they can be accessed by Main () using instance R of the Rectangle class.

Protected access Modifiers

The Protected access modifier allows subclasses to access the member variables and member functions of its base class. This helps to implement inheritance. We will discuss this in detail in the inherited chapters. Discuss this in more detail.

Internal access Modifiers

The Internal access specifier allows a class to expose its member variables and member functions to other functions and objects in the current program. In other words, any member with the internal access modifier can be defined to be accessed by any class or method within the application defined by that member.

This is illustrated in the following example:

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 ("Area: {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 above example, note that the member function Getarea () is declared without any access modifiers. If no access modifier is specified, the default access modifier for the class member is used, which is private.

Protected Internal access modifier

The Protected Internal access modifier allows a class to hide its member variables and member functions from other class objects and functions other than subclasses within the same application. This is also used to implement inheritance.

The above is the "C # Tutorial" C # Package content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.