Csharp 简明教程
C
polymorphism 一词表示具有许多形式。在面向对象编程范式中,多态性通常表示为“一个接口,多个功能”。
The word polymorphism means having many forms. In object-oriented programming paradigm, polymorphism is often expressed as 'one interface, multiple functions'.
多态性可以是静态的或动态的。在 static polymorphism 中,对函数的响应是在编译时确定的。在 dynamic polymorphism 中,它是在运行时决定的。
Polymorphism can be static or dynamic. In static polymorphism, the response to a function is determined at the compile time. In dynamic polymorphism, it is decided at run-time.
Static Polymorphism
在编译时将函数与对象链接的机制称为早期绑定。它也称为静态绑定。C# 提供了两种实现静态多态性的技术。它们是 −
The mechanism of linking a function with an object during compile time is called early binding. It is also called static binding. C# provides two techniques to implement static polymorphism. They are −
-
Function overloading
-
Operator overloading
我们将在下一章中讨论运算符重载。
We discuss operator overloading in next chapter.
Function Overloading
可以在相同范围内为同一函数名称设置多个定义。函数的定义通过参数列表中的类型和/或参数的数量相互区分。您无法重载仅通过返回类型不同的函数声明。
You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You cannot overload function declarations that differ only by return type.
以下示例显示使用函数 print() 来打印不同的数据类型 −
The following example shows using function print() to print different data types −
using System;
namespace PolymorphismApplication {
class Printdata {
void print(int i) {
Console.WriteLine("Printing int: {0}", i );
}
void print(double f) {
Console.WriteLine("Printing float: {0}" , f);
}
void print(string s) {
Console.WriteLine("Printing string: {0}", s);
}
static void Main(string[] args) {
Printdata p = new Printdata();
// Call print to print integer
p.print(5);
// Call print to print float
p.print(500.263);
// Call print to print string
p.print("Hello C++");
Console.ReadKey();
}
}
}
编译并执行上述代码后,将产生以下结果 −
When the above code is compiled and executed, it produces the following result −
Printing int: 5
Printing float: 500.263
Printing string: Hello C++
Dynamic Polymorphism
C# 允许你创建抽象类,这些类用于提供接口的部分类实现。当派生类从它继承时,实现就完成了。 Abstract 类包含抽象方法,这些方法由派生类实现。派生类具有更专门的功能。
C# allows you to create abstract classes that are used to provide partial class implementation of an interface. Implementation is completed when a derived class inherits from it. Abstract classes contain abstract methods, which are implemented by the derived class. The derived classes have more specialized functionality.
以下是关于抽象类的规则 −
Here are the rules about abstract classes −
-
You cannot create an instance of an abstract class
-
You cannot declare an abstract method outside an abstract class
-
When a class is declared sealed, it cannot be inherited, abstract classes cannot be declared sealed.
以下程序演示了一个抽象类 −
The following program demonstrates an abstract class −
using System;
namespace PolymorphismApplication {
abstract class Shape {
public abstract 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 class area :");
return (width * length);
}
}
class RectangleTester {
static void Main(string[] args) {
Rectangle r = new Rectangle(10, 7);
double a = r.area();
Console.WriteLine("Area: {0}",a);
Console.ReadKey();
}
}
}
编译并执行上述代码后,将产生以下结果 −
When the above code is compiled and executed, it produces the following result −
Rectangle class area :
Area: 70
当你有一个在类中定义的函数,并且希望在继承的类中实现它时,可以使用 virtual 函数。虚拟函数可以在不同的继承类中以不同的方式实现,并且对这些函数的调用将在运行时决定。
When you have a function defined in a class that you want to be implemented in an inherited class(es), you use virtual functions. The virtual functions could be implemented differently in different inherited class and the call to these functions will be decided at runtime.
动态多态性是通过 abstract classes 和 virtual functions 实现的。
Dynamic polymorphism is implemented by abstract classes and virtual functions.
以下程序演示了这一点 −
The following program demonstrates this −
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("Parent class area :");
return 0;
}
}
class Rectangle: Shape {
public Rectangle( int a = 0, int b = 0): base(a, b) {
}
public override int area () {
Console.WriteLine("Rectangle class area :");
return (width * height);
}
}
class Triangle: Shape {
public Triangle(int a = 0, int b = 0): base(a, b) {
}
public override int area() {
Console.WriteLine("Triangle class area :");
return (width * height / 2);
}
}
class Caller {
public void CallArea(Shape sh) {
int a;
a = sh.area();
Console.WriteLine("Area: {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();
}
}
}
编译并执行上述代码后,将产生以下结果 −
When the above code is compiled and executed, it produces the following result −
Rectangle class area:
Area: 70
Triangle class area:
Area: 25