運(yùn)算符重載
基本概念
- 我們可以重定義或重載 C# 中內(nèi)置的運(yùn)算符
- 可以使用用戶自定義類型的運(yùn)算符
- 重載運(yùn)算符是具有 特殊名稱的函數(shù) ,是通過(guò)關(guān)鍵字 ** operator **后跟運(yùn)算符的符號(hào)來(lái)定義的
- 與其他函數(shù)一樣,重載運(yùn)算符有****返回類型和參數(shù)列表
- 使用 operator 關(guān)鍵字來(lái)聲明運(yùn)算符。 運(yùn)算符聲明必須符合以下規(guī)則
- 同時(shí)包含 public 和 static 修飾符
- 一元運(yùn)算符有一個(gè)輸入?yún)?shù),二元運(yùn)算符有兩個(gè)輸入?yún)?shù)
- **語(yǔ)法示例: 正面的class 表示用戶自定義的類,以下是重載 + **
public static Class operator+ (Class a, Class b)
使用案例
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Car car1 = new Car();
car1.length=2;
car1.speed = 120;
Car car2 = new Car();
car2.length = 3;
car2.speed = 90;
// 將兩個(gè)car對(duì)象相加
Car c3 = car1 + car2;
Console.WriteLine(c3); // output: length:5,speed:210
}
}
public class Car
{
///
/// 長(zhǎng)度
///
public int length;
///
/// 速度
///
public int speed;
// 重載 對(duì)象的 +
public static Car operator +(Car c1, Car c2)
{
Car c3 = new Car();
c3.length = c1.length + c2.length;
c3.speed = c1.speed + c2.speed;
return c3;
}
// 重寫tostring方法,方便打印對(duì)象信息
public override string ToString()
{
return string.Format("length:{0},speed:{1}", this.length, this.speed);
}
}
}
**如果不使用重載也想要達(dá)成以上效果,作法如下:
**
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Car car1 = new Car();
car1.length=2;
car1.speed = 120;
Car car2 = new Car();
car2.length = 3;
car2.speed = 90;
// 將兩個(gè)car對(duì)象相加
Car car3 = new Car();
car3.length = car1.length + car2.length;
car3.speed = car1.speed + car2.speed;
Console.WriteLine(car3); // output: length:5,speed:210
}
}
public class Car
{
///
/// 長(zhǎng)度
///
public int length;
///
/// 速度
///
public int speed;
// 重載 對(duì)象的 +
public static Car operator +(Car c1, Car c2)
{
Car c3 = new Car();
c3.length = c1.length + c2.length;
c3.speed = c1.speed + c2.speed;
return c3;
}
// 重寫tostring方法,方便打印對(duì)象信息
public override string ToString()
{
return string.Format("length:{0},speed:{1}", this.length, this.speed);
}
}
}
可以看到效果,如果不重載運(yùn)算符,則需要在外面調(diào)用時(shí) 自己去計(jì)算,以上只有一處調(diào)用,是否重載無(wú)所謂,但是如果 調(diào)用的地方多了,每個(gè)調(diào)用的地方都得寫此邏輯就太麻煩了,所以用好運(yùn)算符重載還是很好用的
重載多個(gè)運(yùn)算符實(shí)現(xiàn)
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Car car1 = new Car();
car1.length = 2;
car1.speed = 120;
// 利用重載的 true/false 運(yùn)算符
if (car1)
{
Console.WriteLine(car1); // output: length:2,speed:120
}
Car car2 = new Car();
car2.length = 3;
car2.speed = 90;
// 將兩個(gè)car對(duì)象相加
Car c3 = car1 + car2;
Console.WriteLine(c3); // output: length:5,speed:210
// 相減
c3 = car1 - car2;
Console.WriteLine(c3); // output: length:-1,speed:30
// 比較
if (car1 != car2)
{
Console.WriteLine("我們不相等");
}
}
}
public class Car
{
///
/// 長(zhǎng)度
///
public int length;
///
/// 速度
///
public int speed;
// 重載 對(duì)象的 +
public static Car operator +(Car c1, Car c2)
{
Car c3 = new Car();
c3.length = c1.length + c2.length;
c3.speed = c1.speed + c2.speed;
return c3;
}
// 重載 對(duì)象的 -
public static Car operator -(Car c1, Car c2)
{
Car c3 = new Car();
c3.length = c1.length - c2.length;
c3.speed = c1.speed - c2.speed;
return c3;
}
// 重載 對(duì)象的 ==, 注 == 和 != 是一對(duì)的,重寫了一個(gè),兩個(gè)就都必須重寫
public static bool operator ==(Car c1, Car c2)
{
return c1.length == c2.length && c1.speed == c2.speed;
}
// 重載 對(duì)象的 !=
public static bool operator !=(Car c1, Car c2)
{
return c1.length != c2.length || c1.speed != c2.speed;
}
// 重載 對(duì)象的 true, true和false也是一對(duì)
public static bool operator true(Car c1)
{
return c1.length != 0 || c1.speed != 0;
}
// 重載 對(duì)象的 false
public static bool operator false(Car c1)
{
return c1.length == 0 && c1.speed == 0;
}
// 重寫tostring方法,方便打印對(duì)象信息
public override string ToString()
{
return string.Format("length:{0},speed:{1}", this.length, this.speed);
}
}
}
可重載和不可重載的運(yùn)算****符
可被重載的 | 不可被重載的 |
---|---|
+, -, !, ~, ++, --,*, /, % | &&, |
==, !=, <, >, <=, >= | =, ., ?:, ->, new, is, sizeof, typeof |
-
函數(shù)
+關(guān)注
關(guān)注
3文章
4327瀏覽量
62569 -
運(yùn)算符
+關(guān)注
關(guān)注
0文章
172瀏覽量
11079 -
重載
+關(guān)注
關(guān)注
0文章
7瀏覽量
2761
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論