[. NET object-oriented programming fundamentals] (11) Object-oriented three major features--encapsulation
Our topic is object-oriented programming, the first is to introduce the basic object-oriented knowledge, and from here is the core of object-oriented, that is,
Object-oriented three major features: encapsulation, inheritance, polymorphism.
1. Encapsulation Concept
Encapsulation: Each object contains all the information that it can manipulate, a feature called Encapsulation . Such a method is included in the class and is implemented through an instance of the class.
2. Advantages of Encapsulation
A. Good encapsulation reduces coupling (e.g. interface and logic separation)
B. Can let the class external interface unchanged, the internal can achieve free modification
C. class has a clear external interface, the user only needs to call, do not care about the internal
D. Because the encapsulated class function is relatively independent, it is better to implement code reuse
E. Can protect the code from inadvertent destruction, through private fields, such as internal implementation. Note: The code protection here does not refer to the encryption of the code itself, but rather to the private implementation of code that does not want to change externally.
In order to better understand the encapsulation we give a few practical examples
instance One: There's a nice house( Houseclass), there are: beds in the house(BedProperties), Sofa (Sofaproperties), desk(DeskProperties), till(BoxProperties), and a door.(DoorProperties), Windows(WindowProperties), we can use a class and his attribute members to represent the object of this House, before we define it, we can consider that things like the till, the bed, personal privacy, can not casually let the passing people see, it is private. Nor can the window be closed to accept the sun, then the window is a public property. And the door to leave home when need to lock, back home need to open, this is an external method, of course, the lock is required to have a set of conditions, such as the key. With a description, let's implement this package in code.
1 /// <summary>2 ///House Class3 /// </summary>4 class House5 {6 Object_bed;7 Object_sofa;8 Object_desk;9 Object_box;Ten Object_door; One Object_window; A - /// <summary> - ///bed Default Private member the /// </summary> - ObjectBed - { - Get{return_bed;} + Set{_bed =value;} - } + /// <summary> A ///Sofa (private) at /// </summary> - ObjectSofa - { - Get{return_sofa;} - Set{_sofa =value;} - } in /// <summary> - ///Desk (private) to /// </summary> + ObjectDesk - { the Get{return_desk;} * Set{_desk =value;} $ }Panax Notoginseng /// <summary> - ///till (private) the /// </summary> + ObjectBox A { the Get{return_box;} + Set{_box =value;} - } $ $ /// <summary> - ///Door (public) - /// </summary> the Public ObjectDoor - {Wuyi Get{return_door;} the Set{_door =value;} - } Wu - /// <summary> About ///windows (Public) $ /// </summary> - Public ObjectWindow - { - Get{return_window;} A Set{_window =value;} + } the}
Example two: We have a unit to raise a value of $ , Yellow Shepherd, when strangers come, will call a few sound, of course, the more unfamiliar the higher the number of calls. The processing of such a transaction can take advantage of the object-oriented encapsulation feature , The code is implemented as follows:
1 /// <summary>2 ///Dog Class3 /// </summary>4 classDog5 {6 string_dogname;7 ReadOnlyColor _dogcolor=Color.yellow;8 ReadOnly Double_dogprice =800.00;9 int_shoutnumber;Ten One /// <summary> A ///constructor Function - /// </summary> - /// <param name= "Dogname" ></param> the PublicDog (stringdogname) - { -Dogname =Dogname; - } + - /// <summary> + ///Dog Name Public property A /// </summary> at Public stringDogname - { - Get{return_dogname;} - Set{_dogname =value;} - } - /// <summary> in ///Dog Color Read-only property (color is this color when it is born, it only provides read-only, of course, it does not consider staining) - /// </summary> to PublicColor Dogcolor + { - Get{return_dogcolor;} the } * /// <summary> $ ///Dog Price Read-only property (this is the price bought, history is not modifiable)Panax Notoginseng /// </summary> - Public DoubleDogprice the { + Get{return_dogprice;} A } the + /// <summary> - ///Number of barking dogs (different strangers come, different number of calls) $ /// </summary> $ Public intshoutnumber{ - Set{_shoutnumber=value;} - Get{return_shoutnumber;} the } - Wuyi /// <summary> the ///the way the dog barks - /// </summary> Wu Public voidShout () - { About intI=0; $ stringshoutnum=""; - Do - { -Shoutnum + ="Wang! "; Ai++; +} while(I <=shoutnumber); the MessageBox.Show (shoutnum); - } $}
The dog class above is all about the dog, including its price, color, call, name, etc. encapsulated into a class to facilitate external invocation. A read-only attribute is used for the price and color of the dog, because the historical price and color it buys are not changed (hair dye is not considered). Through the idea of encapsulation we can increase the weight of the dog and other public properties, without affecting the external interface calls, the private read-only properties are protected.
We believe that through these two examples, we are fully familiar with encapsulation. If our dog gave birth to another baby, many of his characteristics are basically the same, do we have to copy this class again? This will use another important feature of object-oriented-inheritance, which we'll introduce slowly.
International practice, we summarize the main points of the package
Points:
1. Encapsulation is to put together all the information that an object can manipulate
2. Encapsulation can reduce coupling, maintain a unified external interface, internal modification does not affect external calls
==============================================================================================
Back to Catalog
==============================================================================================
[. NET object-oriented programming fundamentals] (11) Object-oriented three major features--encapsulation