In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to understand the encapsulation of C#". In the daily operation, I believe that many people have doubts about how to understand the encapsulation of C#. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to understand the encapsulation of C#"! Next, please follow the editor to study!
Catalogue
1. Preface
2. Examples
1. Preface
Access to data by the method. In addition, you can also control the way to access data. In object-oriented programming, most of them take class as the basic unit of data encapsulation. Class combines the data and the methods that manipulate the data into one unit. When designing a class, you do not want to access the data in the class directly, but you want to access the data through methods. In this way, we can achieve the purpose of encapsulating the data, facilitate future maintenance and upgrade, and also make an extra layer of judgment when operating the data.
In addition, encapsulation can also solve the problem of data access permissions. You can use encapsulation to hide the data to form a closed question, and then you can set which data can only be used in this space and which data can be used outside the space. A class contains sensitive data that some people can access and some people cannot access, and if the access to this data is not restricted, the consequences will be very strict, so when writing a program, use different access modifiers for the members of the class to define their access level.
The purpose of encapsulation is to enhance security and simplify programming, and users do not need to know the specific implementation details, but only to use the members of the class through the specific access rights of the external interface. Such as the charger, it is the 220V power supply through step-down rectifier filter, with a wire connected to the battery, and then charge. The process of step-down rectifier filtering is equivalent to class encapsulation.
2. Examples
[example 1] create a console application in which you customize a MyClass class that encapsulates the additive and appended properties. Then customize an Add method that returns the sum of the two int properties in the class. In the Program main program class, instantiate the object of the custom class and assign values to the two properties in the MyClass class. Finally, the sum of the two properties is returned by calling the custom method Add in the MyClass class.
The code is as follows:
Class MyClass / / Custom class that encapsulates additive and appended attributes {prvate intx=0: / / defines int variable as additive private int y = 0; / / defines int variable as additive / publie intx {get {return x;} set {x = value;}} public int y / / appended get {return y;} set {y * value }} public int Add () / / summation {retum Xeroy} class Program {static void Main (string (] args) {MyClass myclass u new MyClass (); / / instantiate the object myclass.x=3; of MyClass / / to the default value of the attribute in the MyClass class myclass.Y = 5; / / assign Console.WriteLine (myclass.Add ()) to the attribute in the MyClass class: / / call the Add method in the MyClass class to sum Console.ReadLine ();}}
The running result is 8
At this point, the study on "how to understand the encapsulation of C# class" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.