C#多态
来源:互联网 发布:玫瑰花茶 知乎 编辑:程序博客网 时间:2024/06/07 14:59
在面向对象编程范式中,多态性往往表现为"一个接口,多个功能"。
多态性可以是静态的或动态的。
在静态多态性中,函数的响应是在编译时发生的。
在动态多态性中,函数的响应是在运行时发生的。
在编译时,函数和对象的连接机制被称为早期绑定,也被称为静态绑定。C# 提供了两种技术来实现静态多态性。分别为:
函数重载
您可以在同一个范围内对相同的函数名有多个定义。函数的定义必须彼此不同,可以是参数列表中的参数类型不同,也可以是参数个数不同。不能重载只有返回类型不同的函数声明。
下面的实例演示了几个相同的函数 print(),用于打印不同的数据类型:
- 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();
-
- p.print(5);
-
- p.print(500.263);
-
- p.print("Hello C++");
- Console.ReadKey();
- }
- }
- }
当上面的代码被编译和执行时,它会产生下列结果:
- Printing int: 5
- Printing float: 500.263
- Printing string: Hello C++
运算符重载
您可以重定义或重载 C# 中内置的运算符。因此,程序员也可以使用用户自定义类型的运算符。重载运算符是具有特殊名称的函数,是通过关键字 operator 后跟运算符的符号来定义的。与其他函数一样,重载运算符有返回类型和参数列表。
例如,请看下面的函数:
- public static Box operator+ (Box b, Box c)
- {
- Box box = new Box();
- box.length = b.length + c.length;
- box.breadth = b.breadth + c.breadth;
- box.height = b.height + c.height;
- return box;
- }
上面的函数为用户自定义的类 Box 实现了加法运算符(+)。它把两个 Box 对象的属性相加,并返回相加后的 Box 对象。
下面的程序演示了完整的实现:
- using System;
-
- namespace OperatorOvlApplication
- {
- class Box
- {
- private double length;
- private double breadth;
- private double height;
-
- public double getVolume()
- {
- return length * breadth * height;
- }
- public void setLength( double len )
- {
- length = len;
- }
-
- public void setBreadth( double bre )
- {
- breadth = bre;
- }
-
- public void setHeight( double hei )
- {
- height = hei;
- }
-
- public static Box operator+ (Box b, Box c)
- {
- Box box = new Box();
- box.length = b.length + c.length;
- box.breadth = b.breadth + c.breadth;
- box.height = b.height + c.height;
- return box;
- }
-
- }
-
- class Tester
- {
- static void Main(string[] args)
- {
- Box Box1 = new Box();
- Box Box2 = new Box();
- Box Box3 = new Box();
- double volume = 0.0;
-
-
- Box1.setLength(6.0);
- Box1.setBreadth(7.0);
- Box1.setHeight(5.0);
-
-
- Box2.setLength(12.0);
- Box2.setBreadth(13.0);
- Box2.setHeight(10.0);
-
-
- volume = Box1.getVolume();
- Console.WriteLine("Box1 的体积: {0}", volume);
-
-
- volume = Box2.getVolume();
- Console.WriteLine("Box2 的体积: {0}", volume);
-
-
- Box3 = Box1 + Box2;
-
-
- volume = Box3.getVolume();
- Console.WriteLine("Box3 的体积: {0}", volume);
- Console.ReadKey();
- }
- }
- }
当上面的代码被编译和执行时,它会产生下列结果:
- Box1 的体积: 210
- Box2 的体积: 1560
- Box3 的体积: 5400
下表描述了 C# 中运算符重载的能力:
运算符 | 描述 | +, -, !, ~, ++, --这些一元运算符只有一个操作数,且可以被重载。+, -, *, /, %这些二元运算符带有两个操作数,且可以被重载。==, !=, <, >, <=, >=这些比较运算符可以被重载。&&, ||这些条件逻辑运算符不能被直接重载。+=, -=, *=, /=, %=这些赋值运算符不能被重载。=, ., ?:, ->, new, is, sizeof, typeof这些运算符不能被重载。针对上述讨论,让我们扩展上面的实例,重载更多的运算符:
- using System;
-
- namespace OperatorOvlApplication
- {
- class Box
- {
- private double length;
- private double breadth;
- private double height;
-
- public double getVolume()
- {
- return length * breadth * height;
- }
- public void setLength( double len )
- {
- length = len;
- }
-
- public void setBreadth( double bre )
- {
- breadth = bre;
- }
-
- public void setHeight( double hei )
- {
- height = hei;
- }
-
- public static Box operator+ (Box b, Box c)
- {
- Box box = new Box();
- box.length = b.length + c.length;
- box.breadth = b.breadth + c.breadth;
- box.height = b.height + c.height;
- return box;
- }
-
- public static bool operator == (Box lhs, Box rhs)
- {
- bool status = false;
- if (lhs.length == rhs.length && lhs.height == rhs.height
- && lhs.breadth == rhs.breadth)
- {
- status = true;
- }
- return status;
- }
- public static bool operator !=(Box lhs, Box rhs)
- {
- bool status = false;
- if (lhs.length != rhs.length || lhs.height != rhs.height
- || lhs.breadth != rhs.breadth)
- {
- status = true;
- }
- return status;
- }
- public static bool operator <(Box lhs, Box rhs)
- {
- bool status = false;
- if (lhs.length < rhs.length && lhs.height
- < rhs.height && lhs.breadth < rhs.breadth)
- {
- status = true;
- }
- return status;
- }
-
- public static bool operator >(Box lhs, Box rhs)
- {
- bool status = false;
- if (lhs.length > rhs.length && lhs.height
- > rhs.height && lhs.breadth > rhs.breadth)
- {
- status = true;
- }
- return status;
- }
-
- public static bool operator <=(Box lhs, Box rhs)
- {
- bool status = false;
- if (lhs.length <= rhs.length && lhs.height
- <= rhs.height && lhs.breadth <= rhs.breadth)
- {
- status = true;
- }
- return status;
- }
-
- public static bool operator >=(Box lhs, Box rhs)
- {
- bool status = false;
- if (lhs.length >= rhs.length && lhs.height
- >= rhs.height && lhs.breadth >= rhs.breadth)
- {
- status = true;
- }
- return status;
- }
- public override string ToString()
- {
- return String.Format("({0}, {1}, {2})", length, breadth, height);
- }
-
- }
-
- class Tester
- {
- static void Main(string[] args)
- {
- Box Box1 = new Box();
- Box Box2 = new Box();
- Box Box3 = new Box();
- Box Box4 = new Box();
- double volume = 0.0;
-
-
- Box1.setLength(6.0);
- Box1.setBreadth(7.0);
- Box1.setHeight(5.0);
-
-
- Box2.setLength(12.0);
- Box2.setBreadth(13.0);
- Box2.setHeight(10.0);
-
-
- Console.WriteLine("Box1: {0}", Box1.ToString());
- Console.WriteLine("Box2: {0}", Box2.ToString());
-
-
- volume = Box1.getVolume();
- Console.WriteLine("Box1 的体积: {0}", volume);
-
-
- volume = Box2.getVolume();
- Console.WriteLine("Box2 的体积: {0}", volume);
-
-
- Box3 = Box1 + Box2;
- Console.WriteLine("Box3: {0}", Box3.ToString());
-
- volume = Box3.getVolume();
- Console.WriteLine("Box3 的体积: {0}", volume);
-
-
- if (Box1 > Box2)
- Console.WriteLine("Box1 大于 Box2");
- else
- Console.WriteLine("Box1 不大于 Box2");
- if (Box1 < Box2)
- Console.WriteLine("Box1 小于 Box2");
- else
- Console.WriteLine("Box1 不小于 Box2");
- if (Box1 >= Box2)
- Console.WriteLine("Box1 大于等于 Box2");
- else
- Console.WriteLine("Box1 不大于等于 Box2");
- if (Box1 <= Box2)
- Console.WriteLine("Box1 小于等于 Box2");
- else
- Console.WriteLine("Box1 不小于等于 Box2");
- if (Box1 != Box2)
- Console.WriteLine("Box1 不等于 Box2");
- else
- Console.WriteLine("Box1 等于 Box2");
- Box4 = Box3;
- if (Box3 == Box4)
- Console.WriteLine("Box3 等于 Box4");
- else
- Console.WriteLine("Box3 不等于 Box4");
-
- Console.ReadKey();
- }
- }
- }
当上面的代码被编译和执行时,它会产生下列结果:
- Box1: (6, 7, 5)
- Box2: (12, 13, 10)
- Box1 的体积: 210
- Box2 的体积: 1560
- Box3: (18, 20, 15)
- Box3 的体积: 5400
- Box1 不大于 Box2
- Box1 小于 Box2
- Box1 不大于等于 Box2
- Box1 小于等于 Box2
- Box1 不等于 Box2
- Box3 等于 Box4
动态多态性
C# 允许您使用关键字 abstract 创建抽象类,用于提供接口的部分类的实现。当一个派生类继承自该抽象类时,实现即完成。抽象类包含抽象方法,抽象方法可被派生类实现。派生类具有更专业的功能。
请注意,下面是有关抽象类的一些规则:
- 您不能创建一个抽象类的实例。
- 您不能在一个抽象类外部声明一个抽象方法。
- 通过在类定义前面放置关键字 sealed,可以将类声明为密封类。当一个类被声明为 sealed 时,它不能被继承。抽象类不能被声明为 sealed。
下面的程序演示了一个抽象类:
- 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 类的面积:");
- 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();
- }
- }
- }
当上面的代码被编译和执行时,它会产生下列结果:
当有一个定义在类中的函数需要在继承类中实现时,可以使用虚方法。虚方法是使用关键字 virtual 声明的。虚方法可以在不同的继承类中有不同的实现。对虚方法的调用是在运行时发生的。
动态多态性是通过 抽象类 和 虚方法 实现的。
下面的程序演示了这点:
- 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
virtual和abstract的区别
virtual和abstract都是用来修饰父类的,通过覆盖父类的定义,让子类重新定义。
- 1.virtual修饰的方法必须有实现(哪怕是仅仅添加一对大括号),而abstract修饰的方法一定不能实现。
- 2.virtual可以被子类重写,而abstract必须被子类重写。
- 3.如果类成员被abstract修饰,则该类前必须添加abstract,因为只有抽象类才可以有抽象方法。
- 4.无法创建abstract类的实例,只能被继承无法实例化。
小结
多态:一个接口多个功能。
静态多态性:编译时发生函数响应(调用);
动态多态性:运行时发生函数响应。
静态绑定(早期绑定):编译时函数和对象的连接机制。
两种技术实现静态多态性:函数重载/运算符重载。
函数重载:在同一范围内对相同函数名有多个定义,可以是参数类型或参数个数的不同,但不许只有返回值类型不同。
运算符重载:可以重定义或重载 C# 中内置的运算符
两种技术实现动态多态性:抽象类/虚方法
抽象类:abstract用于接口部分类的实现(派生类继承抽象类时,实现完成)。抽象类包含抽象方法,抽象方法可被派生类实现。
虚方法:virtual用于声明虚方法,虚方法可以在不同的继承类中有不同的实现。
重载(overload):是提供了一种机制, 相同函数名通过不同的返回值类型以及参数来表来区分的机制。
重写(override):是用于重写基类的虚方法,这样在派生类中提供一个新的方法。