设计模式之面向对象基础

    最近看设计模式,这本书我特别喜欢,内容也很好理解,读起来有些上瘾,哈哈,一开始看了看面向对象基础,之前接触过面对对象基础,对面向对象三大特性有了进一步的理解,下面做个总结:

                1、封装

                      概念:每个对象都包含它能进行操作所需要的所有信息,这个特性称为封装。

                      理解:就像一个大盒子,它具有某类事物的共同特性,我需要做的就是将这些共性实例化为个例,比如Cat类实例化为名为Tom的一只猫。

                      优点:1、减少耦合,即可以减少对外界的依赖和联系,尽可能独立;2、类内部的实现可以自由地修改,但会减少对外界的影响;3、类具有清晰的对外接口;

      例子:这里的两个类是互不影响的,它们互不依赖。

                      

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;">using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10.   
  11. namespace 封装  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.   
  20.         private void button1_Click(object sender, EventArgs e)  
  21.         {  
  22.             Cat cat = new Cat("dandan");  
  23.             cat.ShoutNum = 5;  
  24.             MessageBox.Show(cat.Shout());  
  25.         }  
  26.         class Cat  
  27.         {  
  28.             private string name = "";  
  29.             public Cat (string name)  
  30.             {  
  31.                 this.name = name;  
  32.             }  
  33.             public Cat()  
  34.             {  
  35.                 this.name = "丹丹";  
  36.             }  
  37.             public string Shout()  
  38.             {  
  39.                 string result = "";  
  40.                 for (int i = 0; i < shoutNum;i++ )  
  41.                 {  
  42.                     result += "喵";  
  43.                 }  
  44.                 return "我的名字叫" + name + "喵";  
  45.             }  
  46.             public int shoutNum = 3;  
  47.             public int ShoutNum  
  48.             {  
  49.                 get  
  50.                 {  
  51.                     return shoutNum;  
  52.                 }  
  53.                 set  
  54.                 {  
  55.                     if (value <= 10)  
  56.                         shoutNum = value;  
  57.                     else  
  58.                         shoutNum = 10;  
  59.                 }  
  60.             }  
  61.         }  
  62.         private void button2_Click_1(object sender, EventArgs e)  
  63.         {  
  64.             Dog dog = new Dog("dandan");  
  65.             dog.ShoutNum = 5;  
  66.             MessageBox.Show(dog.Shout());  
  67.         }  
  68.          class Dog  
  69.          {  
  70.              private string name = "";  
  71.              public Dog(string name)  
  72.              {  
  73.                  this.name = name;  
  74.              }  
  75.              public Dog()  
  76.              {  
  77.                  this.name = "丹丹";  
  78.              }  
  79.              public string Shout()  
  80.              {  
  81.                  string result = "";  
  82.                  for (int i = 0; i < shoutNum; i++)  
  83.                  {  
  84.                      result += "汪";  
  85.                  }  
  86.                  return "我的名字叫" + name + "汪";  
  87.              }  
  88.              public int shoutNum = 3;  
  89.              public int ShoutNum  
  90.              {  
  91.                  get  
  92.                  {  
  93.                      return shoutNum;  
  94.                  }  
  95.                  set  
  96.                  {  
  97.                      if (value <= 10)  
  98.                          shoutNum = value;  
  99.                      else  
  100.                          shoutNum = 10;  
  101.                  }  
  102.              }  
  103.          }  
  104.   
  105.         
  106.     }  
  107. }  
  108. </span>  


                2、继承

                           概念:对象的继承代表了一种‘is-a’的关系,如果两个对象A和B,可以描述为‘B是A’,则表明B可以继承A。

                          理解:比如钢笔是文具,则钢笔继承了文具,但是钢笔除了具有文具的特征外还有他的特性,比如需要用钢笔水,笔尖是钢制的。再比如儿子和父亲,儿子具有父亲的一些外貌的特征,但是儿子本身身高要高,大眼睛,这些就是新的特性,父亲在面向对象中也叫父类或基类,而儿子则叫子类或派生类。

                          优点:使得所有子类公共的部分都放在了父类,使得代码得到了共享,这就避免了重复,并且,继承可使得修改或扩展而来的实现都较为容易。

                          注意:1、子类拥有父类非private的属性和功能(即拥有public和protected);2、子类具有自己的属性和功能,即子类可以扩展父类没有的属性和功能;3、子类可以以自己的方法实现父类的功能(方法重写)。

                           例子:我想大家都看到上面的例子有大量的重复代码,这样不但增加代码量,而且很容易出错,所以我们就用到了继承,将它们共同的部分提取出来称为父类,让Cat和Dog都去继承。下面是改进后的代码,对比一下就能体会出继承的作用了。

                           

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;">using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10.   
  11. namespace 继承  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.         class Animal  
  20.         {  
  21.             protected string name = "";  
  22.             public Animal (string name)  
  23.             {  
  24.                 this.name = name;  
  25.             }  
  26.             public Animal ()  
  27.             {  
  28.                 this.name = "无名";  
  29.             }  
  30.             protected int shoutNum = 3;  
  31.             public int ShoutNum  
  32.             {  
  33.                 get  
  34.                 {  
  35.                     return shoutNum;  
  36.                 }  
  37.                 set  
  38.                 {  
  39.                     shoutNum = value;  
  40.                 }  
  41.             }  
  42.         }  
  43.         private void button1_Click(object sender, EventArgs e)  
  44.         {  
  45.             Cat cat = new Cat("dandan");  
  46.             cat.ShoutNum = 5;  
  47.             MessageBox.Show(cat.Shout());  
  48.         }  
  49.         class Cat:Animal   
  50.         {  
  51.             public Cat ():base()  
  52.             { }  
  53.             public Cat (string name):base(name )  
  54.             { }  
  55.             public string Shout()  
  56.             {  
  57.                 string result = "";  
  58.                 for (int i = 0; i < shoutNum; i++)  
  59.                 {  
  60.                     result += "喵";  
  61.                 }  
  62.                 return "我的名字叫" + name + " "+result ;  
  63.             }  
  64.         }  
  65.         private void button2_Click(object sender, EventArgs e)  
  66.         {  
  67.             Dog dog = new Dog("dandan");  
  68.             dog.ShoutNum = 5;  
  69.             MessageBox.Show(dog.Shout());  
  70.         }  
  71.         class Dog : Animal  
  72.         {  
  73.             public Dog()  
  74.                 : base()  
  75.             { }  
  76.             public Dog(string name)  
  77.                 : base(name)  
  78.             { }  
  79.             public string Shout()  
  80.             {  
  81.                 string result = "";  
  82.                 for (int i = 0; i < shoutNum; i++)  
  83.   
  84.                     result += "汪";  
  85.   
  86.                 return "我的名字叫" + name + " " + result;  
  87.             }  
  88.         }  
  89.     }  
  90. }  
  91. </span>  
                  3、多态

                           概念:不同的对象可以执行相同的动作,但要通过它们自己的代码实现。

                           理解:比如说我们代码中举得例子,猫狗都有叫,但是叫的声音不同,那么我们就可以写一个叫的方法,这个方法为父类,但是父类是不能直接去实现猫狗的叫声的,所以需要将父类虚化即用virtual,然后子类去重写这个方法即用override。

                          例子:在这个例子中因为运用到了多态,所以在运行结果中就会出现在动物报名完成后,叫声比赛中每个动物会自动与其相对应的叫声结合,即将Shout()方法重写,得到正确的叫声。

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;">using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10.   
  11. namespace 多态  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.         class Animal  
  20.         {  
  21.             protected string name = "";  
  22.             public Animal(string name)  
  23.             {  
  24.                 this.name = name;  
  25.             }  
  26.             public Animal()  
  27.             {  
  28.                 this.name = "无名";  
  29.             }  
  30.             protected int shoutNum = 3;  
  31.             public int ShoutNum  
  32.             {  
  33.                 get  
  34.                 {  
  35.                     return shoutNum;  
  36.                 }  
  37.                 set  
  38.                 {  
  39.                     shoutNum = value;  
  40.                 }  
  41.             }  
  42.             public virtual string Shout()  
  43.             {  
  44.                 return "";  
  45.             }  
  46.         }  
  47.         private void button1_Click(object sender, EventArgs e)  
  48.         {  
  49.             Cat cat = new Cat("dandan");  
  50.             cat.ShoutNum = 5;  
  51.             MessageBox.Show(cat.Shout());  
  52.         }  
  53.         class Cat : Animal  
  54.         {  
  55.             public Cat()  
  56.                 : base()  
  57.             { }  
  58.             public Cat(string name)  
  59.                 : base(name)  
  60.             { }  
  61.             public override string Shout()  
  62.             {  
  63.                 string result = "";  
  64.                 for (int i = 0; i < shoutNum; i++)  
  65.                   
  66.                     result += "喵";  
  67.                   
  68.                 return "我的名字叫" + name + " " + result;  
  69.             }  
  70.         }  
  71.         private void button2_Click(object sender, EventArgs e)  
  72.         {  
  73.             Dog dog = new Dog("dandan");  
  74.             dog.ShoutNum = 5;  
  75.             MessageBox.Show(dog.Shout());  
  76.         }  
  77.         class Dog : Animal  
  78.         {  
  79.             public Dog()  
  80.                 : base()  
  81.             { }  
  82.             public Dog(string name)  
  83.                 : base(name)  
  84.             { }  
  85.             public override string Shout()  
  86.             {  
  87.                 string result = "";  
  88.                 for (int i = 0; i < shoutNum; i++)  
  89.   
  90.                     result += "汪";  
  91.   
  92.                 return "我的名字叫" + name + " " + result;  
  93.             }  
  94.         }  
  95.   
  96.         private void Form1_Load(object sender, EventArgs e)  
  97.         {  
  98.   
  99.         }  
  100.         private Animal[] arrayAnimal;//动物数组  
  101.         //动物报名  
  102.         private void button3_Click(object sender, EventArgs e)  
  103.         {  
  104.             arrayAnimal = new Animal[2];  
  105.             arrayAnimal[0] = new Cat("小花");  
  106.             arrayAnimal [1]=new Dog ("丹丹")  
  107.               
  108.         }  
  109.   
  110.         private void button4_Click(object sender, EventArgs e)  
  111.         {  
  112.             foreach (Animal item in arrayAnimal )//遍历数组,让猫狗都Shout()  
  113.             {  
  114.                 MessageBox.Show(item.Shout());  
  115.             }  
  116.         }  
  117.       
  118.     }  
  119. }  
  120. </span>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值