版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/advent86/article/details/83545801
- 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();
编译运行后结果如下: