One of C # object-oriented cores: encapsulation

Source: Internet
Author: User

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.

Encapsulation Example: Our unit has a value of 800 yuan, 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 Class 3//</summary> 4 class Dog 5 {6 string _dogname; 7 readonly Color _dogco Lor=color.yellow; 8 readonly Double _dogprice = 800.00; 9 int _shoutnumber;10///&LT;SUMMARY&GT;12//constructor///</summary>14//<param name= "D Ogname "></param>15 public Dog (string dogname) + {dogname = dogname;18}19//<s UMMARY&GT;21///Dog Name Public properties///</summary>23 publicly string DogName24 {get {return _dogn Ame }26 set {_dogname = value;} }28///<summary>29///Dog Color read-only attribute (color is the color of birth, here only read-only, of course, do not consider dyeing)//</summary>31 PU            Blic Color DogColor32 {get {return _dogcolor;}  }35///<summary>36///Dog price read-only property (this is the price purchased, history is not modified) PNS//</summary>38 public double DogPrice39 {+ get {return _dogprice;} }42//<summARY&GT;44///dog barks (different strangers come, different number of calls)//</summary>46 public int shoutnumber{47 Set{_shoutnum Ber=value;} get {return _shoutnumber;}         }50////&LT;SUMMARY&GT;52////</summary>54 public void shout () 55 {56 int i=0;57 string shoutnum= "", do59 {shoutnum + = "Wang! "; i++;62} while (I <= shoutnumber); MessageBox.Show (shoutnum); 64} 65}

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.

It is believed that through these two instances, the encapsulation has been completely familiar. 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 is introduced slowly.

Summarize the essentials 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

One of C # object-oriented cores: encapsulation

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.