C# 的静态多态性和动态多态性(学习心得 20)
同一个行为,具有多个不同表现形式或形态的能力。
在面向对象编程范式中,多态性往往表现为”一个接口,多个功能”。
多态性可以是静态的或动态的:
-
静态多态性中,函数的响应是在 编译 时发生的。
-
动态多态性中,函数的响应是在 运行 时发生的。
在 C# 中,每个类型都是多态的,因为包括用户定义类型在内的所有类型都继承自 Object。
同一个事件发生在不同的对象上会产生不同的结果。
一、静态多态性
在编译时,函数 和 对象 的 连接机制 被称为:早期绑定(静态绑定)。
C# 中两种技术实现静态多态性:
- 函数重载
- 运算符重载
1.1 函数重载
同一个范围内对相同的函数名有多个定义。
函数的定义必须彼此不同,可以是参数列表中的 参数类型 不同,也可以是 参数个数 不同。
不能重载只有返回类型不同的函数声明。
例:不同参数个数
using System;
namespace PolymorphismApplication
{
public class TestData
{
public int Add(int a, int b, int c)
{
return a + b + c;
}
public int Add(int a, int b)
{
return a + b;
}
}
class Program
{
static void Main(string[] args)
{
TestData dataClass = new TestData();
int add1 = dataClass.Add(1, 2);
int add2 = dataClass.Add(1, 2, 3);
Console.WriteLine("add1 :" + add1);
Console.WriteLine("add2 :" + add2);
}
}
}
运行结果:
add1 :3
add2 :6
例:不同参数类型
using System;
namespace PolymorphismApplication
{
class Printdata
{
void print(int i)
{
Console.WriteLine("输出整型: {0}", i );
}
void print(double f)
{
Console.WriteLine("输出浮点型: {0}" , f);
}
void print(string s)
{
Console.WriteLine("输出字符串: {0}", s);
}
static void Main(string[] args)
{
Printdata p = new Printdata();
// 调用 print 来打印整数
p.print(1);
// 调用 print 来打印浮点数
p.print(1.23);
// 调用 print 来打印字符串
p.print("Hello Runoob");
Console.ReadKey();
}
}
}
运行结果:
输出整型: 1
输出浮点型: 1.23
输出字符串: Hello Runoob
二、动态多态性
2.1 抽象类
使用关键字 abstract 创建抽象类,用于提供接口的部分类的实现。
当一个 派生类 继承自该抽象类时,实现即完成。
**抽象类 **包含 抽象方法,抽象方法可被派生类实现。
派生类具有更专业的功能。
关于抽象类的规则:
- 不能创建一个抽象类的实例。
- 不能在一个抽象类外部,声明一个抽象方法。
- 抽象类不能被声明为 sealed。(通过在类定义前面放置关键字 sealed,可以将类声明为密封类。当一个类被声明为 sealed 时,它不能被继承。)
抽象类 和 接口 的区别(来源于网络):
抽象类 | 接口 |
---|---|
由子类继承 | 由类实现 |
包含:方法声明,方法实现 | 包含:方法声明 |
变量:普通变量 | 变量:公共静态变量 |
抽象级别低 | 抽象级别高 |
包含:方法,属性 | 包含:抽象方法,不可变常量 |
用于抽象类别 | 用于抽象功能 |
用 abstract 定义 | 用 interface 定义 |
子类只能继承一个抽象类 | 类可以实现多个接口 |
子类必须实现抽象方法 | 类必须实现接口的所有成员 |
我自己理解的为什么要使用抽象类:
- 通用实现和专业实现分离。
- 抽象类中可以实现方法,约束继承它的类。
例:
using System;
namespace PolymorphismApplication
{
abstract class Shape
{
abstract public int area();
}
class Rectangle: Shape
{
private int length;
private int width;
public Rectangle( int a=0, int b=0)
{
length = a;
width = b;
}
public override int area () // 这里需要用到 override
{
Console.WriteLine("Rectangle 类的面积:");
return (width * length);
}
}
class RectangleTester
{
static void Main(string[] args)
{
Rectangle r = new Rectangle(10, 7);
double a = r.area();
Console.WriteLine("面积: {0}",a);
Console.ReadKey();
}
}
}
运行结果:
Rectangle 类的面积:
面积: 70
2.2 虚方法
当有一个定义在类中的函数需要在继承类中实现时,可以使用虚方法。
使用关键字 virtual 声明。
虚方法可以在不同的继承类中有不同的实现。
对虚方法的调用是在 运行 时发生的。
例:创建 Shape 基类,并创建派生类 Circle、 Rectangle、Triangle, Shape 类提供一个名为 Draw 的虚拟方法,在每个派生类中重写该方法以绘制该类的指定形状。
using System;
using System.Collections.Generic;
public class Shape
{
public int X { get; private set; }
public int Y { get; private set; }
public int Height { get; set; }
public int Width { get; set; }
// 虚方法
public virtual void Draw()
{
Console.WriteLine("执行基类的画图任务");
}
}
class Circle : Shape
{
public override void Draw()
{
// 基类的base.Draw()不是必须运行的
Console.WriteLine("画一个圆形");
}
}
class Rectangle : Shape
{
public override void Draw()
{
Console.WriteLine("画一个长方形");
base.Draw();
}
}
class Triangle : Shape
{
public override void Draw()
{
base.Draw();
Console.WriteLine("画一个三角形");
}
}
class Program
{
static void Main(string[] args)
{
// 创建一个 List<Shape> 对象,并向该对象添加 Circle、Triangle 和 Rectangle
var shapes = new List<Shape>
{
new Rectangle(),
new Triangle(),
new Circle()
};
// 使用 foreach 循环对该列表的派生类进行循环访问,并对其中的每个 Shape 对象调用 Draw 方法
foreach (var shape in shapes)
{
shape.Draw();
}
Console.WriteLine("按下任意键退出。");
Console.ReadKey();
}
}
运行结果:
画一个长方形
执行基类的画图任务
执行基类的画图任务
画一个三角形
画一个圆形
按下任意键退出。
例:通过虚方法 area() 来计算不同形状图像的面积
using System;
namespace PolymorphismApplication
{
class Shape
{
protected int width, height;
public Shape( int a=0, int b=0)
{
width = a;
height = b;
}
public virtual int area()
{
Console.WriteLine("父类的面积:");
return 0;
}
}
class Rectangle: Shape
{
public Rectangle( int a=0, int b=0): base(a, b)
{
}
public override int area ()
{
Console.WriteLine("Rectangle 类的面积:");
return (width * height);
}
}
class Triangle: Shape
{
public Triangle(int a = 0, int b = 0): base(a, b)
{
}
public override int area()
{
Console.WriteLine("Triangle 类的面积:");
return (width * height / 2);
}
}
class Caller
{
public void CallArea(Shape sh)
{
int a;
a = sh.area();
Console.WriteLine("面积: {0}", a);
}
}
class Tester
{
static void Main(string[] args)
{
Caller c = new Caller();
Rectangle r = new Rectangle(10, 7);
Triangle t = new Triangle(10, 5);
c.CallArea(r);
c.CallArea(t);
Console.ReadKey();
}
}
}
运行结果:
Rectangle 类的面积:
面积:70
Triangle 类的面积:
面积:25