[. Net Object-Oriented Programming Basics] (11) three major object-oriented features-encapsulation and. net Object-Oriented Programming

Source: Internet
Author: User

[. Net Object-Oriented Programming Basics] (11) three major object-oriented features-encapsulation and. net Object-Oriented Programming

[. Net Object-Oriented Programming Basics] (11) three major object-oriented features-encapsulation

Our topic is object-oriented programming. We have introduced the basic knowledge of object-oriented, and from here we are the core part of object-oriented, that is

Object-oriented features: encapsulation, inheritance, and polymorphism.

1. encapsulation Concept

Encapsulation: each object contains all the information it can perform operations on. This feature is called encapsulation.. This method is included in the class and implemented through the class instance.

2. Advantages of encapsulation

A. Good encapsulation can reduce coupling (such as implementing interface and logical separation)

B. The class's external interface can be kept unchanged and can be freely modified internally.

C. The class has clear external interfaces, so you only need to call the class without worrying about the internal

D. Because the encapsulated class functions are relatively independent, code reuse can be better implemented.

E. It can protect the code from being accidentally destroyed and implement internal implementation through private fields. Note: The code protection here is not the encryption of the Code itself, but the private Implementation of code that does not require external changes.

To better understand encapsulation, let's give a few practical examples.

Instance 1: There is a beautiful House (House type), which includes: Bed, Sofa, Desk, and Box ), there are also Doors and windows. We can use a class and its property members to represent the House object. Before defining it, we can consider, private things such as money cabinets and beds cannot be seen by people passing by. They are private. The window cannot be closed and the sun is not accepted. Therefore, the window is a public attribute. The door needs to be locked when leaving the house, and the door needs to be opened when returning home. This is an external method. Of course, unlocking requires a fixed condition, such as a key. Through a description, we can use the code to implement this encapsulation.

1 /// <summary> 2 // House class 3 /// </summary> 4 class House 5 {6 object _ bed; 7 object _ sofa; 8 object _ desk; 9 object _ box; 10 object _ door; 11 object _ window; 12 13 /// <summary> 14 // default private member of the bed 15 /// </summary> 16 object Bed17 {18 get {return _ bed ;} 19 set {_ bed = value;} 20} 21 // <summary> 22 // sofa (private) 23 // </summary> 24 object Sofa25 {26 get {return _ sofa;} 27 set {_ sofa = value ;} 28} 29 // <summary> 30 // desk (private) 31 // </summary> 32 object counter 33 {34 get {return _ desk ;} 35 set {_ desk = value;} 36} 37 // <summary> 38 // cash box (private) 39 // </summary> 40 object Box41 {42 get {return _ box;} 43 set {_ box = value ;} 44} 45 46 // <summary> 47 // door (public) 48 // </summary> 49 public object Door50 {51 get {return _ door ;} 52 set {_ door = value;} 53} 54 55 // <summary> 56 // window (public) 57 // </summary> 58 public object Window59 {60 get {return _ window;} 61 set {_ window = value;} 62} 63}

 

Example 2: Our company raises a yellow sheepdog worth 800 yuan. When a stranger comes, it will scream a few times. Of course, the more unfamiliar the company is, the more requests you call. The object-oriented encapsulation feature can be used to process such a transaction. The code implementation is as follows:

1 /// <summary> 2 // Dog class 3 /// </summary> 4 class Dog 5 {6 string _ dogName; 7 readonly Color _ dogColor = Color. yellow; 8 readonly double _ douplice = 800.00; 9 int _ shoutNumber; 10 11 /// <summary> 12 /// constructor 13 /// </summary> 14 /// <param name = "dogName"> </param> 15 public dog (string dogName) 16 {17 DogName = dogName; 18} 19 20 /// <summary> 21 // dog name public attribute 22 /// </summary> 23 public string DogName24 {25 Get {return _ dogName;} 26 set {_ dogName = value ;} 27} 28 /// <summary> 29 // dog color read-only attribute (color is the color when it is born, only read-only is provided here, of course, dyeing is not considered here) 30 /// </summary> 31 public Color DogColor32 {33 get {return _ dogColor ;} 34} 35 // <summary> 36 // dog price read-only attribute (this is the purchased price and cannot be modified in History) 37 // </summary> 38 public double douplice39 {40 get {return _ douplice ;} 41} 42 43 // <summary> 44 // Number of dog calls (different strangers call different times) 45 // </summary> 46 publ Ic int ShoutNumber {47 set {_ shoutNumber = value;} 48 get {return _ shoutNumber ;} 49} 50 51 // <summary> 52 // Method for calling a dog 53 // </summary> 54 public void Shout () 55 {56 int I = 0; 57 string ShoutNum = ""; 58 do59 {60 ShoutNum + = "Wang! "; 61 I ++; 62} while (I <= ShoutNumber); 63 MessageBox. Show (ShoutNum); 64} 65}

 

The above Dog class is all information about the Dog, including its price, color, sound, name, and so on, encapsulated into a class to facilitate external calls. The read-only attribute is used for the price and color of a dog because the historical price and color bought by the dog cannot be changed ). Through encapsulation, we can increase dog weight and other public attributes without affecting external interface calls and protect private read-only attributes.

We believe that we are completely familiar with encapsulation through these two instances. If our dog has another baby, and many of his features are basically the same, do we have to copy this class again? This requires another important feature of object-oriented-inheritance. We will introduce it later.

In international practice, let's summarize the key points of encapsulation.

Key points:

1. encapsulation is to put all the information that an object can operate on together

2. encapsulation reduces coupling and maintains a unified external interface. Internal modifications do not affect external calls.

 

========================================================== ========================================================== ====================

Returned directory

========================================================== ========================================================== ====================

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.