using System;
namespace ConsoleApp1
{
class Box
{
private double length;
private double breadth;
private double height;
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;
}
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 BoxE
{
private double length;
private double breadth;
private double height;
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;
}
public static BoxE operator + (BoxE b, BoxE c)
{
BoxE box = new BoxE();
box.length = b.length + c.length;
box.breadth = b.breadth + c.breadth;
box.height = b.height + c.height;
return box;
}
public static bool operator == (BoxE lhs, BoxE 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 != (BoxE lhs, BoxE 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 <(BoxE lhs, BoxE 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 >(BoxE lhs, BoxE 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 <=(BoxE lhs, BoxE 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 >=(BoxE lhs, BoxE 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 Program
{
static void Main(string[] args)
{
//运算符重载
//可以重定义或重载C#内置的运算符。重载运算符是具有特殊名称的函数,是通过关键字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;
* return box;
* }
* 上面的函数为用户自定义的类Box实现了加法运算符(+)。他把两个Box对象的属性相加,并返回相加后的Box对象。
*/
Box box1 = new Box();
Box box2 = new Box();
double volume = 0.0;
box1.setLength(6.0);
box1.setBreadth(7.0);
box1.setHeight(5.0);
box2.setLength(12.0);
box2.setBreadth(13.0);
box2.setHeight(10.0);
volume = box1.getVolume();
Console.WriteLine("box1的体积: {0}", volume);
volume = box2.getVolume();
Console.WriteLine("box2的体积: {0}", volume);
Box box3 = box1 + box2;
volume = box3.getVolume();
Console.WriteLine("box3的体积:{0}", volume);
//可重载和不可重载运算符
/*
* + - ! ~ ++ -- 这些一元运算符只有一个操作数,且可以被重载。
* + - * / % 这些二元运算符带有两个操作数,且可以被重载。
* == != < > <= >= 这些比较运算符可以被重载。
* && || 这些条件逻辑运算符可以不能被直接重载。
* += -= *= /= %= 这些赋值运算符不能被直接重载。
* = . ?: -> new is sizeof typeof 这些运算符不能被重载。
*/
BoxE box4 = new BoxE();
BoxE box5 = new BoxE();
double volume2 = 0.0;
box4.setLength(6.0);
box4.setBreadth(7.0);
box4.setHeight(5.0);
box5.setLength(12.0);
box5.setBreadth(13.0);
box5.setHeight(10.0);
Console.WriteLine("box4: {0}", box4.ToString());
Console.WriteLine("box5: {0}", box5.ToString());
volume2 = box4.getVolume();
Console.WriteLine("box4的体积:{0}", volume2);
volume2 = box5.getVolume();
Console.WriteLine("box5的体积:{0}", volume2);
BoxE box6 = box4 + box5;
Console.WriteLine("box6: {0}", box6.ToString());
volume2 = box6.getVolume();
Console.WriteLine("box6的体积:{0}", volume2);
if (box4 > box5)
Console.WriteLine("box4 大于 box5");
else
Console.WriteLine("box4 不大于 box5");
if (box4 < box5)
Console.WriteLine("box4 小于 box5");
else
Console.WriteLine("box4 不小于 box5");
if (box4 >= box5)
Console.WriteLine("box4 大于等于 box5");
else
Console.WriteLine("box4 不大于等于 box5");
if (box4 <= box5)
Console.WriteLine("box4 小于等于 box5");
else
Console.WriteLine("box4 不小于等于 box5");
if (box4 != box5)
Console.WriteLine("box4 不等于 box5");
else
Console.WriteLine("box4 等于 box5");
BoxE box7 = box6;
if (box7 == box6)
Console.WriteLine("box7 等于 box6");
else
Console.WriteLine("box7 不等于 box6");
Console.ReadKey();
}
}
}