C#每日一課(十九)
阿新 • • 發佈:2018-11-04
- C#運算子過載
可以對C#中內建的運算子進行過載。使用自定義型別的運算子。過載運算子是具有特殊名稱的函式,通過關鍵字operator後跟運算子的符號來定義的。與其它函式一樣,過載運算子有返回型別和引數列表。
使用Visual Studio新建C#控制檯應用程式chapter14_001
新增類Box
class Box { private double length; //長 private double width; //寬 private double height; //高 //獲取Box體積 public double getVolume() { return this.length * this.width * this.height; } public void setLength(double length) { this.length = length; } public void setWidth(double width) { this.width = width; } public void setHeight(double height) { this.height = height; } //過載+運算子 public static Box operator+(Box a,Box b) { Box box = new Box(); box.length = a.length + b.length; box.width = a.width + b.width; box.height = a.height + b.height; return box; } }
在Main方法中加入如下測試程式碼:
Box b1 = new Box(); Box b2 = new Box(); Box b3 = new Box(); double volume = 0.0; b1.setLength(3.0); b1.setWidth(4.0); b1.setHeight(5.0); b2.setLength(6.0); b2.setWidth(7.0); b2.setHeight(8.0); b3 = b1 + b2; Console.WriteLine("b1的體積:{0}\nb2的體積:{1}\nb3的體積:{2}", b1.getVolume(),b2.getVolume(),b3.getVolume()); Console.ReadKey();
編譯執行後結果如下:
可過載和不可過載的運算子
- +,-,!,~,++,–:一元運算子,可以被過載
- +,-,*,/,%:二元運算子,可以被過載
- ==,!=,<,>,<=,>=:比較運算子可以被過載
- &&,||:條件邏輯運算子,不可以被過載
- +=,-=,*=,/=,%=:這些載值運算子不可以被過載
- =,.,?:,->,new,is,sizeof,typeof:不可以被過載
過載==和!=運算子(過載時這兩個運算子一定要同時進行)
在Box類中加入如下程式碼:
//過載==運符算,必須同時過載!= public static bool operator == (Box a, Box b) { string str; Console.WriteLine("比較方式(side/volume):"); str = Console.ReadLine(); bool result = false; if (str == "side") { if (a.length == b.length && a.width == b.width && a.height == b.height) { result = true; } } else if (str == "volume") { if (a.getVolume() == b.getVolume()) { result = true; } } return result; } //過載!=運算子 public static bool operator != (Box a, Box b) { string str; Console.WriteLine("比較方式(side/volume):"); str = Console.ReadLine(); bool result = true; if (str == "side") { if (a.length == b.length && a.width == b.width && a.height == b.height) { result = false; } } else if (str == "volume") { if (a.getVolume() == b.getVolume()) { return false; } } else { result = false; } return result; }
過載<和>運算子(過載時這兩個運算子要一同進行)
//過載<運算子,必須同時過載>
public static bool operator < (Box a, Box b)
{
bool result = true;
if (a.getVolume() > b.getVolume())
{
result = false;
}
return result;
}
//過載>運算子
public static bool operator > (Box a, Box b)
{
bool result = false;
if (a.getVolume() > b.getVolume())
{
result = true;
}
return result;
}
在Main方法中加入如下測試程式碼:
Box b4, b5;
b4 = new Box();
b5 = new Box();
b4.setLength(1.0);
b4.setWidth(2.0);
b4.setHeight(3.0);
b5.setLength(2.0);
b5.setWidth(1.0);
b5.setHeight(3.0);
Console.WriteLine("-------------過載比較運算子-------------");
Console.WriteLine("b4.length={0},b4.width={1},b4.height={2}\nb5.length={3},b5.width={4},b5.height={5}" , b4.getLength(), b4.getWidth(), b4.getHeight(), b5.getLength(), b5.getWidth(), b5.getHeight());
Console.WriteLine("b4 == b5比較結果:{0}", b4 == b5);
Console.WriteLine("b4 != b5比較結果:{0}", b4 != b5);
Console.WriteLine("b4 > b5比較結果:{0}", b4 > b5);
Console.WriteLine("b4 < b5比較結果:{0}", b4 < b5);
Console.ReadKey();
編譯執行後結果如下: