encapulation ):
The following describes how to combine data and methods.
method and data:
two important features of encapsulation:
1. place the required data and methods in the same scope.
2. pay attention to the accessibility of data and methods.
traditional procedural programming languages, such as C language, include a lot of data and functions. Each function can access part of the data, for small Programs , data and functions can work well. However, as program functions increase, program availability increases. Data changes may result in invalid methods for applicable data. The more complex the program is, the less likely it is to change data. Programs become difficult to change, but for developers, changes are inevitable and frequent.
therefore, separation of data from functions is extremely important. Another reason why data must be separated from functions is that developers often use models to describe their own ideas, rather than computer ideas.
Object-Oriented Programming allows developers to easily establish idea models and describe computer ideas using human ideas.
abandoning procedural programming and moving towards object-oriented programming, the first and most important step is to combine data and functions.
Control accessibility:
as shown in the left figure, withdraw, balance, deposit is placed in a container named bandaccount (Entity). If you use it to describe a bank account, it is very bad because balance can be accessed from outside without security, the solution is shown in the figure on the right. Using encapsulation, data and functions are put together to form a closed area, so that there is a zone inside and outside, and different accessibility settings are set, only restricted access areas outside the region are allowed. for sensitive data, it is set to allow only internal access, prohibit external access, and increase security. You can operate the balance function and allow external access. The change to the balance can only be done by withdrao and deposit. A function that allows external use. It is defined as public. data that does not allow external access is defined as private.
C # allow users to define the accessibility of members in the container and declare public data. However, it is not recommended that the data be accessed only by Members in the container.
for data defined as private type, it can be called abstract data type (ADTs). This type cannot access the data Composition Form, only accessible methods can be used. The built-in data type in C #, such as int, is ADTs. When adding two int data variables, you do not have to consider the binary structure of the two int data, you only need to know the operator +.
when the accessibility of members is set to public, different views in the same access area can be created ).
An external view is a subset of an internal view (subset ). There is a lot of discussion about putting new features inside or outside the container. We put new features inside the container and keep them inaccessible, which makes the effect better.
Why encapsulate)
Because:
1. allow control. data can be operated by a separate method.
When you are driving a car, you only need to consider how to make assumptions, you must consider the car structure; when you get the money at the bank, you do not have to consider how the Bank Account operates. By using encapsulation and methods, you can design software objects and let them work as you want.
2. Change and private data update are allowed without affecting external access.
Although the construction of trucks and cars is different, if you drive a truck, you will also drive a car. When the bank deposits change, such as sending a salary or opening a new account, the usage is the same.
If you want to declare a method as public or private, select private because the effect of changing private to public is smaller, and changing public to priate has a much greater impact.
Object Data:
The majority of data in an object describes the information in each object. Different bank accounts have balance, which is generally different.
Private Data in the object, which can only be accessed by methods in the object. Encapsulation and isolation are effective self-contained objects.
Use static data:
Each bank account has an interest rate. It is obviously not a good idea to store interest rates for each account.
Because:
1. Under normal circumstances, the interest rates of each account are the same;
2. Occupying too much hard disk space;
3. If the interest rate is updated, it is obviously time-consuming;
4. added the class size. Private Data in the class, there must be a public method to access.
The solution is not to declare the interest rate at the object level, but to declare it at the class level, so that it becomes public data.
Note: The defined public data does not exist in the class and cannot be encapsulated. Therefore, public data cannot be used in object-oriented languages including C # unless declared as static.
Static data is declared in the class (static, compile-time entity), benefiting from class encapsulation, but logically only related to the same class itself, but not to the class instance. In other words, although static data is declared in the class according to the syntax, it exists before the class is instantiated..
Use static method ):
You can use static methods to encapsulate static data ). As shown in, the interest rate is related to the Account type and is irrelevant to the individual case. Static methods can use and modify interest rates at the class level.
Static declaration methods are similar to static life data. They all use static modifiers. Static methods also exist at the class level and are only related to classes. Public and private can be used to control the accessibility of static methods and static data.
Because static methods are only related to the same type, you can only use static methods at the class level. This means that static methods cannot use the this keyword, because this implicitly points to the object that calls this method, not the class. Class, including static data and other static methods.
Static Methods retain the methods that allow all private members in the response class, that is, they access other non-static members by referencing objects.
For example:
Class Time
{
Public Static Void Reset (time t)
{
T. Hours = 0 ; // Okay
T. Minutes = 0 ; // Okay
Hour = 0 ; // Compile-time error
Minute = 0 // Compile-time error
}
Private Int Hour, minute;
}
The original Article is more exciting.
Reference: msdn Training
Introduction to C # Programming
For the Microsoft. NET platform
(Prerelease)
Workbook
Course number: 2124a