在编译期间将函数与对象的链接称为静态绑定。C#提供了两种实现静态多态的技术:函数重载和运算符重载。
在“函数重载”中,您可以在同一作用域中为同一函数名称具有多个定义。
void print(int i) { Console.WriteLine("Printing int: {0}", i ); } void print(double f) { Console.WriteLine("Printing float: {0}" , f); }
重载运算符是具有特殊名称的函数。关键字运算符IS后面紧跟定义的运算符符号D。
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; }