最近看设计模式,这本书我特别喜欢,内容也很好理解,读起来有些上瘾,哈哈,一开始看了看面向对象基础,之前接触过面对对象基础,对面向对象三大特性有了进一步的理解,下面做个总结:
1、封装
概念:每个对象都包含它能进行操作所需要的所有信息,这个特性称为封装。
理解:就像一个大盒子,它具有某类事物的共同特性,我需要做的就是将这些共性实例化为个例,比如Cat类实例化为名为Tom的一只猫。
优点:1、减少耦合,即可以减少对外界的依赖和联系,尽可能独立;2、类内部的实现可以自由地修改,但会减少对外界的影响;3、类具有清晰的对外接口;
例子:这里的两个类是互不影响的,它们互不依赖。
- <span style="font-size:18px;">using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace 封装
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Cat cat = new Cat("dandan");
- cat.ShoutNum = 5;
- MessageBox.Show(cat.Shout());
- }
- class Cat
- {
- private string name = "";
- public Cat (string name)
- {
- this.name = name;
- }
- public Cat()
- {
- this.name = "丹丹";
- }
- public string Shout()
- {
- string result = "";
- for (int i = 0; i < shoutNum;i++ )
- {
- result += "喵";
- }
- return "我的名字叫" + name + "喵";
- }
- public int shoutNum = 3;
- public int ShoutNum
- {
- get
- {
- return shoutNum;
- }
- set
- {
- if (value <= 10)
- shoutNum = value;
- else
- shoutNum = 10;
- }
- }
- }
- private void button2_Click_1(object sender, EventArgs e)
- {
- Dog dog = new Dog("dandan");
- dog.ShoutNum = 5;
- MessageBox.Show(dog.Shout());
- }
- class Dog
- {
- private string name = "";
- public Dog(string name)
- {
- this.name = name;
- }
- public Dog()
- {
- this.name = "丹丹";
- }
- public string Shout()
- {
- string result = "";
- for (int i = 0; i < shoutNum; i++)
- {
- result += "汪";
- }
- return "我的名字叫" + name + "汪";
- }
- public int shoutNum = 3;
- public int ShoutNum
- {
- get
- {
- return shoutNum;
- }
- set
- {
- if (value <= 10)
- shoutNum = value;
- else
- shoutNum = 10;
- }
- }
- }
- }
- }
- </span>
2、继承
概念:对象的继承代表了一种‘is-a’的关系,如果两个对象A和B,可以描述为‘B是A’,则表明B可以继承A。
理解:比如钢笔是文具,则钢笔继承了文具,但是钢笔除了具有文具的特征外还有他的特性,比如需要用钢笔水,笔尖是钢制的。再比如儿子和父亲,儿子具有父亲的一些外貌的特征,但是儿子本身身高要高,大眼睛,这些就是新的特性,父亲在面向对象中也叫父类或基类,而儿子则叫子类或派生类。
优点:使得所有子类公共的部分都放在了父类,使得代码得到了共享,这就避免了重复,并且,继承可使得修改或扩展而来的实现都较为容易。
注意:1、子类拥有父类非private的属性和功能(即拥有public和protected);2、子类具有自己的属性和功能,即子类可以扩展父类没有的属性和功能;3、子类可以以自己的方法实现父类的功能(方法重写)。
例子:我想大家都看到上面的例子有大量的重复代码,这样不但增加代码量,而且很容易出错,所以我们就用到了继承,将它们共同的部分提取出来称为父类,让Cat和Dog都去继承。下面是改进后的代码,对比一下就能体会出继承的作用了。
- <span style="font-size:18px;">using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace 继承
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- class Animal
- {
- protected string name = "";
- public Animal (string name)
- {
- this.name = name;
- }
- public Animal ()
- {
- this.name = "无名";
- }
- protected int shoutNum = 3;
- public int ShoutNum
- {
- get
- {
- return shoutNum;
- }
- set
- {
- shoutNum = value;
- }
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Cat cat = new Cat("dandan");
- cat.ShoutNum = 5;
- MessageBox.Show(cat.Shout());
- }
- class Cat:Animal
- {
- public Cat ():base()
- { }
- public Cat (string name):base(name )
- { }
- public string Shout()
- {
- string result = "";
- for (int i = 0; i < shoutNum; i++)
- {
- result += "喵";
- }
- return "我的名字叫" + name + " "+result ;
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- Dog dog = new Dog("dandan");
- dog.ShoutNum = 5;
- MessageBox.Show(dog.Shout());
- }
- class Dog : Animal
- {
- public Dog()
- : base()
- { }
- public Dog(string name)
- : base(name)
- { }
- public string Shout()
- {
- string result = "";
- for (int i = 0; i < shoutNum; i++)
- result += "汪";
- return "我的名字叫" + name + " " + result;
- }
- }
- }
- }
- </span>
概念:不同的对象可以执行相同的动作,但要通过它们自己的代码实现。
理解:比如说我们代码中举得例子,猫狗都有叫,但是叫的声音不同,那么我们就可以写一个叫的方法,这个方法为父类,但是父类是不能直接去实现猫狗的叫声的,所以需要将父类虚化即用virtual,然后子类去重写这个方法即用override。
例子:在这个例子中因为运用到了多态,所以在运行结果中就会出现在动物报名完成后,叫声比赛中每个动物会自动与其相对应的叫声结合,即将Shout()方法重写,得到正确的叫声。
- <span style="font-size:18px;">using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace 多态
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- class Animal
- {
- protected string name = "";
- public Animal(string name)
- {
- this.name = name;
- }
- public Animal()
- {
- this.name = "无名";
- }
- protected int shoutNum = 3;
- public int ShoutNum
- {
- get
- {
- return shoutNum;
- }
- set
- {
- shoutNum = value;
- }
- }
- public virtual string Shout()
- {
- return "";
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Cat cat = new Cat("dandan");
- cat.ShoutNum = 5;
- MessageBox.Show(cat.Shout());
- }
- class Cat : Animal
- {
- public Cat()
- : base()
- { }
- public Cat(string name)
- : base(name)
- { }
- public override string Shout()
- {
- string result = "";
- for (int i = 0; i < shoutNum; i++)
- result += "喵";
- return "我的名字叫" + name + " " + result;
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- Dog dog = new Dog("dandan");
- dog.ShoutNum = 5;
- MessageBox.Show(dog.Shout());
- }
- class Dog : Animal
- {
- public Dog()
- : base()
- { }
- public Dog(string name)
- : base(name)
- { }
- public override string Shout()
- {
- string result = "";
- for (int i = 0; i < shoutNum; i++)
- result += "汪";
- return "我的名字叫" + name + " " + result;
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private Animal[] arrayAnimal;//动物数组
- //动物报名
- private void button3_Click(object sender, EventArgs e)
- {
- arrayAnimal = new Animal[2];
- arrayAnimal[0] = new Cat("小花");
- arrayAnimal [1]=new Dog ("丹丹")
- }
- private void button4_Click(object sender, EventArgs e)
- {
- foreach (Animal item in arrayAnimal )//遍历数组,让猫狗都Shout()
- {
- MessageBox.Show(item.Shout());
- }
- }
- }
- }
- </span>