运算符重载
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test14
{
class Box
{
private double length, breadth, heigth;
public double Length { get; set; }
public double Breadth { get; set; }
public double Heigth { get; set; }
public double getVolume()
{
return Length * Breadth * Heigth;
}
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.Heigth = b.Heigth + c.Heigth;
return box;
}
public static bool operator == (Box lhs, Box rhs)
{
bool status = false;
if(lhs.Length == rhs.Length && lhs.Breadth == rhs.Breadth && lhs.Heigth == rhs.Heigth)
{
status = true;
}
return status;
}
public static bool operator !=(Box lhs, Box rhs)
{
bool status = false;
if(lhs.Length != rhs.Length || lhs.Breadth != rhs.Breadth || lhs.Heigth != rhs.Heigth)
{
status = true;
}
return status;
}
public static bool operator <(Box lhs, Box rhs)
{
bool status = false;
if(lhs.Length < rhs.Length && lhs.Breadth < rhs.Breadth && lhs.Heigth < rhs.Heigth)
{
status = true;
}
return status;
}
public static bool operator > (Box lhs, Box rhs)
{
bool status = false;
if(lhs.Length > rhs.Length && lhs.Breadth > rhs.Breadth && lhs.Heigth > rhs.Heigth)
{
status = true;
}
return status;
}
public static bool operator <=(Box lhs, Box rhs)
{
bool status = false;
if(lhs.Length <= rhs.Length && lhs.Breadth <= rhs.Breadth && lhs.Heigth <= rhs.Heigth)
{
status = true;
}
return status;
}
public static bool operator >= (Box lhs, Box rhs)
{
bool status = false;
if(lhs.Length >= rhs.Length && lhs.Breadth >= rhs.Breadth && lhs.Heigth >= rhs.Heigth)
{
status = true;
}
return status;
}
public override string ToString()
{
return String.Format("({0}, {1}, {2})", Length, Breadth, Heigth);
}
}
class Program
{
static void Main(string[] args)
{
Box box1 = new Box();
Box box2 = new Box();
Box box3 = new Box();
Box box4 = new Box();
double volume = 0.0;
box1.Length = 6.0;
box1.Breadth = 7.0;
box1.Heigth = 5.0;
box2.Length = 12.0;
box2.Breadth = 13.0;
box2.Heigth = 10.0;
Console.WriteLine("box1: {0}", box1.ToString());
Console.WriteLine("box2: {0}", box2.ToString());
volume = box1.getVolume();
Console.WriteLine("box1的体积:{0}", volume);
volume = box2.getVolume();
Console.WriteLine("box2的体积:{0}", volume);
box3 = box1 + box2;
Console.WriteLine("box3: {0}", box3.ToString());
volume = box3.getVolume();
Console.WriteLine("box3的体积:{0}", volume);
if(box1 > box2)
{
Console.WriteLine("box1 > box2");
}
else
{
Console.WriteLine("box1 not> box2");
}
if (box1 < box2)
{
Console.WriteLine("box1 < box2");
}
else
{
Console.WriteLine("box1 not< box2");
}
if (box1 >= box2)
{
Console.WriteLine("box1 >= box2");
}
else
{
Console.WriteLine("box1 not>= box2");
}
if (box1 <= box2)
{
Console.WriteLine("box1 <= box2");
}
else
{
Console.WriteLine("box1 not<= box2");
}
if(box1 != box2)
{
Console.WriteLine("box1不等于box2");
}
else
{
Console.WriteLine("box1 等于 box2");
}
box4 = box3;
if(box3 == box4)
{
Console.WriteLine("box3 等于 box4");
}
else
{
Console.WriteLine("box3 不等于 box4");
}
Console.ReadKey();
}
}
}