C # learning notes-encapsulation,

Source: Internet
Author: User

C # learning notes-encapsulation,

Preface

I am ashamed to say that I have learned C # For more than half a year. In fact, I first came into contact with the encapsulation part, but I have been confused about it, and I feel that I don't care much if it has no influence, now I began to carefully read this part of the content. After reading it, many things are much clearer, so I found that the basis of encapsulation is so important.

Now, on the other hand, encapsulation and classes are actually the definition that the teacher taught when I first learned object-oriented programming. The most basic and fundamental thing I can understand is that, I am also sorry for my previous tuition fee! (Grief !)

However, when I was studying, my teacher held books and I did not practice them. So many things were too empty! Or that sentence: "The end of the paper is shallow, and you must know that this is the case "!

Definition

Encapsulation refers to the collection of data or functions in units.

In my understanding, encapsulation is"Package", As to whether you pack, take it away, package it, or give it to, it is your freedom.

Just like when I go to school, I will pack all the things I want to use into my schoolbag and take them to school, I put all the textbooks, workbooks, pencil cases, laptops, stickers, and so on in one bag. When I go to school, I just want to carry my schoolbag, because all my tools have been "packed", it would be the same if I asked someone to help me bring my schoolbag to school. They don't need to know what is in my schoolbag, they just need to take the bag for me. They can take a long time to get rid of the items in my schoolbag and then throw them, or they can always be there. They can also load new things. Of course, these operations are internal operations in my bag. I only need to know this. People outside of my bag do not care about what happened in the bag.

This is the role of encapsulation: protecting data from being accidentally damaged by external factors, while also facilitating direct calls of external operations.

Use

Actual code operation:

  1    class Program  2     {  3         static void Main(string[] args)  4         {  5             Console.WriteLine("Buy a new car.................");  6             Car car = new Car();  7             Console.WriteLine("Here is the information of new car:");  8             Console.WriteLine("car's color is:{0}", car.Color);  9             Console.WriteLine("car has {0} types", car.TypeNum); 10             Console.WriteLine("car's oil is:{0}\t\n", car.Oil); 11             car.run(); 12             Console.WriteLine("I wanna change the color of car"); 13             car.changeColor(car.Color); 14             car.fillOil(car.Oil); 15             Console.Read(); 16         } 17     } 18  19  20     /// <summary> 21     /// package 22     /// all things about car can be packaged in the one class  23     /// </summary> 24     public class Car 25     { 26         int typeNum = 4; 27         string color = "red"; 28         int oil = 75; 29  30         /// <summary> 31         /// the number of type 32         /// not allowed to modify,onlyread 33         /// </summary> 34         public int TypeNum 35         { 36             get 37             { 38                 return typeNum; 39             } 40         } 41  42         /// <summary> 43         /// the color of car 44         /// but we can change the color 45         /// </summary> 46         public string Color 47         { 48             get 49             { 50                 return color; 51             } 52  53             set 54             { 55                 color = value; 56             } 57         } 58  59         /// <summary> 60         /// the oil 61         /// it always change 62         /// </summary> 63         public int Oil 64         { 65             get 66             { 67                 return oil; 68             } 69  70             set 71             { 72                 oil = value; 73             } 74         } 75  76  77  78         public void run() 79         { 80             Console.WriteLine("Running for a while................\t\n"); 81         } 82  83         public void changeColor(string oldColor) 84         { 85             string newColor = ""; 86             string yORn = ""; 87             Console.WriteLine("Are you sure change the color of your car?Y/N"); 88             yORn = Console.ReadLine(); 89  90             if (yORn == "y" || yORn == "Y") 91             { 92                 Console.WriteLine("Please input which color you wanna"); 93                 newColor = Console.ReadLine(); 94  95                 if (newColor != oldColor) 96                 { 97                     Console.WriteLine("Your car's new color is {0}", newColor); 98                 } 99                 else100                 {101                     Console.WriteLine("Your new color is as same as the old one,so you don't need to change!");102                 }103                 Console.Read();104             }105             else106             {107                 Console.WriteLine("Fine! Your car's color still is{0}", oldColor);108                 Console.Read();109             }110         }111 112 113         public void fillOil(int previousOil)114         {115             int presentOil = 100;116             Console.WriteLine("Your car's oil is{0}%", previousOil);117             Console.WriteLine("Filling the oil.................");118             Console.WriteLine("Now,yourcar's oil is{0}%\t\n", presentOil);119             Console.WriteLine("Fine!Have a nice day");120             Console.Read();121 122         }123     }

Effect preview:

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.