多态(polymorphism)即一个名字具有多种语义。在面向对象中指一个方法可以有多种实现版本。类的多态表现为方法的多态,方法的多态主要有重载(overload)和覆盖(override)。
比如我们用下面的案例来展示一下我们不用多态的时候的劣势
Animal类:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
Dog类:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
Cat类:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
Program类
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
virtual和override实现的多态形式。
现在的话,只有两种动物 貌似看起来没有什么不方便的,但是如果要展示一个动物园的话,那么我们就得写一个庞大的分支语句结构
接下来我们使用virtual和override实现的多态形式。
Aniaml类:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
Dog类
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
Cat类
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
Program类
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
抽象类实现的多态
比如我们现在实现一个平面图形的面积和周长
AbstractTest类:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
Test1 类:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
Test2 类:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
Program类:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
接口实现的多态
对于有些功能,我们不能将其作为一个类去描述的时候,我们可以考虑使用接口来实现。比如我们说 “飞”这样一个功能,我们并不能抽象成一个父类。当然我们第一印象就是鸟类,但是作为鸟类,并不是所有的鸟类都会飞。比如说企鹅,如果我们描述飞机,风筝的时候。那么就更提不上鸟类了,但是我们可以将“飞”作为一个接口。
接口的定义
接口的命名规范,一般接口的名字都是以大写I开头,以后的每个单词的首字母要大写
例如:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
继承自接口的类都必须实现接口中的抽象方法
ButterFly 类:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
Kite 类:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
Plane 类:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
Program类:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
显示实现接口
C#是一种多继承和单继承都存在的语言。只不过单继承针对的是类,多继承针对的是接口。
显示实现接口解决的问题是同一个类继承了多个包含具有多个相同方法名的接口发生冲突的问题
ITest01接口:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
ITest02接口
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
Test 类结成了两个接口
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
实现多态的三种方式的区别
-
第一种方式:
virtual和override的结合构成重写父类的方法
使用场景:对待多个类,可以找到父类,父类对某一个行为也有具体实现的场景 -
第二种方式:
abstract和override的构成重写重写方法
使用场景:对待多个类,可以找到父类,但是父类对该行为不能做出具体的实现 - 第三种方式:
interface接口
使用场景:对待多个类,找不到共同的父类,但是可以找到共同的行为(方法或函数),那么我们可以把该行为封装成一个接口来使用
抽象类和接口的区别
- 相同点:
抽象类和接口都不能实例化
抽象类中的抽象方法和接口中的方法都没有方法体
使用抽象类和接口都可以实现面向对象中的多态 - 不同点:
抽象类是不完全抽象,接口是完全抽象
抽象类中可以有非抽象方法,接口中的方法都是抽象方法
抽象类中的抽象方法可以含有访问修饰符,接口中的方法不含有访问修饰符