c# 多态性
多态是同一个行为具有多个不同表现形式或形态的能力。
多态性意味着有多重形式。在面向对象编程范式中,多态性往往表现为"一个接口,多个功能"。
多态性可以是静态的或动态的。在静态多态性中,函数的响应是在编译时发生的。在动态多态性中,函数的响应是在运行时发生的。
在 c# 中,每个类型都是多态的,因为包括用户定义类型在内的所有类型都继承自 object。
多态就是同一个接口,使用不同的范例而执行不同操作,如图所示:

现实中,比如我们按下 f1 键这个动作:
- 如果当前在 flash 界面下弹出的就是 as 3 的帮助文档;
- 如果当前在 word 下弹出的就是 word 帮助;
- 在 windows 下弹出的就是 windows 帮助和支持。
同一个事件发生在不同的对象上会产生不同的结果。
1. 静态多态性
在编译时,函数和对象的连接机制被称为早期绑定,也被称为静态绑定。c# 提供了两种技术来实现静态多态性。分别为:
- 函数重载
- 运算符重载
运算符重载将在下一章节讨论,接下来我们将讨论函数重载。
2. 函数重载
您可以在同一个范围内对相同的函数名有多个定义。函数的定义必须彼此不同,可以是参数列表中的参数类型不同,也可以是参数个数不同。不能重载只有返回类型不同的函数声明。
下面的范例演示了几个相同的函数 add(),用于对不同个数参数进行相加处理:
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);
}
}
}
下面的范例演示了几个相同的函数 print(),用于打印不同的数据类型:
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 yapf");
console.readkey();
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
输出整型: 1 输出浮点型: 1.23 输出字符串: hello yapf
3. 动态多态性
c# 允许您使用关键字 abstract 创建抽象类,用于提供接口的部分类的实现。当一个派生类继承自该抽象类时,实现即完成。抽象类包含抽象方法,抽象方法可被派生类实现。派生类具有更专业的功能。
请注意,下面是有关抽象类的一些规则:
- 您不能创建一个抽象类的范例。
- 您不能在一个抽象类外部声明一个抽象方法。
- 通过在类定义前面放置关键字 sealed,可以将类声明为密封类。当一个类被声明为 sealed 时,它不能被继承。抽象类不能被声明为 sealed。
下面的程序演示了一个抽象类:
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 ()
{
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
当有一个定义在类中的函数需要在继承类中实现时,可以使用虚方法。
虚方法是使用关键字 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()
{
console.writeline("画一个圆形");
base.draw();
}
}
class rectangle : shape
{
public override void draw()
{
console.writeline("画一个长方形");
base.draw();
}
}
class triangle : shape
{
public override void draw()
{
console.writeline("画一个三角形");
base.draw();
}
}
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


