第一章:C#面向对象编程
第二节:继承和多态
继承可以让class具有一种特殊的能力,即实现class本身定义的所有功能外,还可以对父类(或称为基类、超类)的某些属性和功能进行扩展,这样的类我们称之为子类(或派生类)。
继承有如下的几个规则:
1. 可传递性:
如: C是B的子类,B是A的子类,那么C会同时继承B和A;(object为所有类的基类)
2. 唯扩展性:
子类应是对父类的扩展,而不能在子类除去父类里面的定义;
3. 构造函数和析构函数不能被继承:
除去构造函数和析构函数不能被继承,其他的成员都能被继承(注意区别:能被继承和能被访问,是两个概念)
4. 重载屏蔽性:
如果子类定义了和父类成员同名的新成员,那么会执行重载和覆盖逻辑。但要明白的是,这样做,并不是删除了父类的这些成员,而是不能再访问这些被重载和覆盖的成员而已。
5. 子类可以从父类的虚方法、虚属性以及虚索引器进行重载(即多态)
我们可以借用override关键字对父类的虚方法、虚属性以及虚索引器进行重载;
同时,我们也可以借用override关键字对抽象父类的抽象方法进行重载。
abstract和virtural的区别:
(1) abstract可以修饰class以及abstract class内部的函数、属性和索引器;而virtual不可以修饰class;
(2) abstract修饰函数的时候,必须在abstract class才可以;而且abstract函数或属性不可以在abstract class里面有实现,但virtual必须要有实现。
(3) virtual既可以修饰abstract class里面的函数、属性和索引器,也可以修饰一般class里面的函数、属性和索引器;
6. 子类只能继承一个父类(class),而可以继承多个接口(interface)(多重继承)
下图继承关系,可以让大家更好了解这些概念的意义:
具体的实现代码:
1 public abstract class Creature 2 { 3 private string _spiece; 4 5 public Creature(string spiece) 6 { 7 this._spiece = spiece; 8 } 9 10 public string Spiece 11 { 12 get { return this._spiece; } 13 } 14 15 public abstract void Breath(); 16 }
1 public class Animal : Creature 2 { 3 private bool? _sex; 4 5 public Animal(string spiece) 6 : base(spiece) 7 { 8 this._sex = null; 9 } 10 11 public bool? Sex 12 { 13 get { return _sex; } 14 } 15 16 public virtual void Run() 17 { 18 Console.WriteLine("I am running..."); 19 } 20 21 public override void Breath() 22 { 23 Console.WriteLine("I am breathing by animal respiration..."); 24 } 25 }
1 public class Monkey : Animal 2 { 3 public Monkey(string spiece) 4 : base(spiece) { } 5 6 public void ClimbTree() 7 { 8 Console.WriteLine("I am climbing tree..."); 9 } 10 11 public override void Run() 12 { 13 Console.WriteLine("I am running with two legs, sometimes with four legs..."); 14 } 15 16 public override void Breath() 17 { 18 Console.WriteLine("I am breathing with lung..."); 19 } 20 }
1 public class Fish : Animal 2 { 3 public Fish(string spiece) 4 : base(spiece) { } 5 6 public override void Run() 7 { 8 Console.WriteLine("I am running in the water with fins..."); 9 } 10 11 public override void Breath() 12 { 13 Console.WriteLine("I am breathing with cheek..."); 14 } 15 }
1 public class Plant : Creature 2 { 3 public Plant(string spiece) 4 : base(spiece) { } 5 6 public override void Breath() 7 { 8 Console.WriteLine("I am breathing by plant respiration..."); 9 } 10 }
1 public class Tree : Plant 2 { 3 public Tree(string spiece) 4 : base(spiece) { } 5 6 //重载Breath()内部调用base.Breath(),其实和不写没啥区别,这里只是想告诉大家本质是什么。 7 public override void Breath() 8 { 9 base.Breath(); 10 } 11 }
程序运行测试代码:
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 Creature creature001 = new Animal("Animal"); 6 Console.WriteLine(creature001.Spiece); 7 creature001.Breath(); 8 9 Console.WriteLine("—————————————————————————"); 10 11 Creature creature002 = new Plant("Plant"); 12 Console.WriteLine(creature002.Spiece); 13 creature002.Breath(); 14 15 Console.WriteLine("—————————————————————————"); 16 17 Animal animal001 = new Animal("Animal", true); 18 Console.WriteLine(animal001.Spiece); 19 Console.WriteLine(string.Format("Spiece:{0}; Sex:{1}", animal001.Spiece, GetSexName(animal001.Sex))); 20 animal001.Breath(); 21 animal001.Run(); 22 23 Console.WriteLine("—————————————————————————"); 24 25 Creature monkey001 = new Monkey("Monkey", true); 26 Console.WriteLine(monkey001.Spiece); 27 monkey001.Breath(); 28 29 Console.WriteLine("—————————————————————————"); 30 31 Animal animal002 = new Monkey("Monkey", false); 32 Console.WriteLine(animal002.Spiece); 33 Console.WriteLine(string.Format("Spiece:{0}; Sex:{1}", animal002.Spiece, GetSexName(animal002.Sex))); 34 animal002.Breath(); 35 animal002.Run(); 36 37 Console.WriteLine("—————————————————————————"); 38 39 Creature fish001 = new Fish("Fish", true); 40 Console.WriteLine(fish001.Spiece); 41 monkey001.Breath(); 42 43 Console.WriteLine("—————————————————————————"); 44 45 Animal fish002 = new Fish("Fish", true); 46 Console.WriteLine(fish001.Spiece); 47 Console.WriteLine(string.Format("Spiece:{0}; Sex:{1}", fish002.Spiece, GetSexName(fish002.Sex))); 48 fish002.Breath(); 49 fish002.Run(); 50 51 Console.WriteLine("—————————————————————————"); 52 53 Animal animal004 = new Monkey("Monkey", false); 54 Console.WriteLine(animal004.Spiece); 55 Console.WriteLine(string.Format("Spiece:{0}; Sex:{1}", animal004.Spiece, GetSexName(animal004.Sex))); 56 animal002.Breath(); 57 animal002.Run(); 58 59 Console.WriteLine("—————————————————————————"); 60 61 Monkey jack = new Monkey("Monkey", true); 62 Console.WriteLine(jack.Spiece); 63 Console.WriteLine(string.Format("Spiece:{0}; Sex:{1}", jack.Spiece, GetSexName(jack.Sex))); 64 jack.Breath(); 65 jack.Run(); 66 jack.ClimbTree(); 67 68 Console.ReadLine(); 69 } 70 71 private static string GetSexName(bool? value) 72 { 73 string sexName = null; 74 if (value == null) 75 { 76 sexName = "undefined"; 77 } 78 else 79 { 80 sexName = value.Value ? "male" : "female"; 81 } 82 return sexName; 83 } 84 }
运行结果:
Animal
I am breathing by animal respiration...
—————————————————————————
Plant
I am breathing by plant respiration...
—————————————————————————
Animal
Spiece:Animal; Sex:male
I am breathing by animal respiration...
I am running...
—————————————————————————
Monkey
I am breathing with lung...
—————————————————————————
Monkey
Spiece:Monkey; Sex:female
I am breathing with lung...
I am running with two legs, sometimes with four legs...
—————————————————————————
Fish
I am breathing with lung...
—————————————————————————
Fish
Spiece:Fish; Sex:male
I am breathing with cheek...
I am running in the water with fins...
—————————————————————————
Monkey
Spiece:Monkey; Sex:female
I am breathing with lung...
I am running with two legs, sometimes with four legs...
—————————————————————————
Monkey
Spiece:Monkey; Sex:male
I am breathing with lung...
I am running with two legs, sometimes with four legs...
I am climbing tree...
原文:http://www.cnblogs.com/Min-min/p/5094073.html