C# 中的静态绑定是什么?


在编译期间将函数链接到对象的操作称为静态绑定。C# 提供了两种技术来实现静态多态:函数重载和运算符重载。

在函数重载中,可以在相同作用域中为同一函数名拥有多个定义。

示例

void print(int i) {
   Console.WriteLine("Printing int: {0}", i );
}

void print(double f) {
   Console.WriteLine("Printing float: {0}" , f);
}

重载运算符是具有特殊名称的函数。关键词 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;
}

更新时间: 22-06-2020

914 次浏览

开启你的职业生涯

完成课程即可获得认证

开始
广告
© . All rights reserved.