你可以重新定義或重載大部分 C# 可用的內(nèi)置操作符。因此,程序員也可以使用用戶(hù)定義類(lèi)型的操作符。重載操作符是特殊關(guān)鍵字 operator 其后跟被定義的名字的符號(hào)。像其他函數(shù)一樣,重載操作符也有返回類(lèi)型和參數(shù)列表。
例如,瀏覽如下函數(shù):
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;
}
上述函數(shù)實(shí)現(xiàn)了用戶(hù)定義的 Box 類(lèi)中加運(yùn)算符,它增加了兩個(gè) Box 對(duì)象,并返回這個(gè)結(jié)果 Box 對(duì)象。
下列程序顯示了完整地實(shí)現(xiàn):
using System;
namespace OperatorOvlApplication
{
class Box
{
private double length; // box 的長(zhǎng)度
private double breadth; // box 的寬度
private double height; // box 的高度
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;
}
// 重載 + operator 來(lái)增加兩個(gè) Box 對(duì)象
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(); // 聲明 box1 為 box 類(lèi)型
Box Box2 = new Box(); // 聲明 box2 為 box 類(lèi)型
Box Box3 = new Box(); // 聲明 box3 為 box 類(lèi)型
double volume = 0.0; // 在這里存放 box 的體積
// box 1 詳細(xì)數(shù)據(jù)
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// box 2 詳細(xì)數(shù)據(jù)
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// box 1 的體積
volume = Box1.getVolume();
Console.WriteLine("Volume of Box1 : {0}", volume);
// box 2 的體積
volume = Box2.getVolume();
Console.WriteLine("Volume of Box2 : {0}", volume);
// 將兩個(gè)對(duì)象相加如下:
Box3 = Box1 + Box2;
// box 3 的體積
volume = Box3.getVolume();
Console.WriteLine("Volume of Box3 : {0}", volume);
Console.ReadKey();
}
}
}
編譯執(zhí)行上述代碼,得到如下結(jié)果:
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400
下表列出了 C# 中運(yùn)算符的可重載能力:
操作符 | 描述 |
---|---|
+, -, !, ~, ++, -- | 這些一元運(yùn)算符使用一個(gè)操作數(shù),可以被重載 |
+, -, *, /, % | 這些二元運(yùn)算符使用一個(gè)操作數(shù),可以被重載 |
==, !=, <, >, <=, >= | 這些比較運(yùn)算符,可以被重載 |
&&, | | | 這些條件邏輯運(yùn)算符不可被直接重載 |
+=, -=, *=, /=, %= | 賦值運(yùn)算符不能重載 |
=, ., ?:, ->, new, is, sizeof, typeof | 這些運(yùn)算符不能被重載 |
針對(duì)上述討論,讓我們擴(kuò)展上面的例子,重載更多的操作符:
using System;
namespace OperatorOvlApplication
{
class Box
{
private double length; // box 的長(zhǎng)度
private double breadth; // box 的寬度
private double height; // box 的高度
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;
}
// 重載 + operator 來(lái)增加兩個(gè) Box 對(duì)象
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 override string ToString()
{
return String.Format("({0}, {1}, {2})", length, breadth, height);
}
}
class Tester
{
static void Main(string[] args)
{
Box Box1 = new Box(); // 聲明 box1 為 box 類(lèi)型
Box Box2 = new Box(); // 聲明 box2 為 box 類(lèi)型
Box Box3 = new Box(); // 聲明 box3 為 box 類(lèi)型
Box Box4 = new Box(); // 聲明 box4 為 box 類(lèi)型
double volume = 0.0; // 這里存放 box 的體積
// box 1 詳細(xì)數(shù)據(jù)
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// box 2 詳細(xì)數(shù)據(jù)
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
//使用重載的 ToString() 顯示 Boxes
Console.WriteLine("Box 1: {0}", Box1.ToString());
Console.WriteLine("Box 2: {0}", Box2.ToString());
// box 1 的體積
volume = Box1.getVolume();
Console.WriteLine("Volume of Box1 : {0}", volume);
// box 2 的體積
volume = Box2.getVolume();
Console.WriteLine("Volume of Box2 : {0}", volume);
// 將兩個(gè)對(duì)象相加如下:
Box3 = Box1 + Box2;
Console.WriteLine("Box 3: {0}", Box3.ToString());
// box 3 的體積
volume = Box3.getVolume();
Console.WriteLine("Volume of Box3 : {0}", volume);
//比較 boxes
if (Box1 > Box2)
Console.WriteLine("Box1 is greater than Box2");
else
Console.WriteLine("Box1 is greater than Box2");
if (Box1 = Box2)
Console.WriteLine("Box1 is greater or equal to Box2");
else
Console.WriteLine("Box1 is not greater or equal to Box2");
if (Box1
編譯執(zhí)行上述代碼,得到如下結(jié)果:
Box 1: (6, 7, 5)
Box 2: (12, 13, 10)
Volume of Box1 : 210
Volume of Box2 : 1560
Box 3: (18, 20, 15)
Volume of Box3 : 5400
Box1 is not greater than Box2
Box1 is less than Box2
Box1 is not greater or equal to Box2
Box1 is less or equal to Box2
Box1 is not equal to Box2
Box3 is equal to Box4
更多建議: