C#学习笔记-封装

前言

  说起来惭愧,学了大半年的C#,其实最开始就接触到了封装的部分,但是一直模模糊糊的弄不清楚,也觉得没什么影响就没怎么在意,现在才开始认真的看这部分内容,看懂了过后好多东西清晰了不少,才发现封装这个基础那么那么重要。

  现在反过来一想,封装和类这些其实就是当初最开始学习面向对象编程的时候老师教的定义,最基础的最基础,我居然到现在才弄懂,我也是对不起我以前交的学费啊!(悲痛!)

  但是以前学习的时候,老师也是拿着书本,我也是拿着书本,没有练在手上,所以很多东西都太空洞了!还是那句话:“纸上得来终觉浅,绝知此事要躬行”!

定义

  封装就是将数据或函数等集合在一个个的单元中。

  在我的理解里封装就是“打包”,至于你是打包带走,还是打包扔了,还是打包给谁,都是你的自由。

  就像我要去上学,我就要把所有要用的东西全部装到书包里带走到学校一样,我把所有的教科书、练习册、文具盒、笔记本、便利贴等等全部都放在一个包里,我要去上学,我就执行背上书包的动作就好了,因为我的所有的工具都已经“打包”好了,要是让别人帮我把书包带到学校去也是一样的道理,他们并不需要知道我书包里装了什么,他们只要执行帮我带书包这个动作就好了。我的书包里面的东西他们可以用久了废了然后扔了,也可以一直都在,还可以装入新的东西。当然这些操作是我书包里面的内部操作,这个只需要我知道就好了,外面的人他们并不关心里面到底发生了什么。

  这就是封装的作用:保护数据不被外来因素无意间破坏,同时却也方便外面的操作直接调用。

使用

   实际代码操作:

  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}	
", 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................	
");
 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                 else
100                 {
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             else
106             {
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}%	
", presentOil);
119             Console.WriteLine("Fine!Have a nice day");
120             Console.Read();
121 
122         }
123     }

  效果预览:

原文地址:https://www.cnblogs.com/Aries-rong/p/6183202.html