创建控制台应用程序,重载运算符+
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OPeratorOvlApplication
{
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 setHeigth(double hei)
{
height = hei;
}
public static Box operator+(Box b,Box c)//重载+运算符吧两个Box对象加起来
{
Box box = new Box();
box.length = b.length + c.length;
box.height = b.height + c.height;
box.breadth = b.breadth + c.breadth;
return box;
}
}
class Program
{
static void Main(string[] args)
{
Box Box1 = new Box();//box1
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeigth(5.0);
Box Box2 = new Box();//box2
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeigth(10.0);
double volume = Box1.getVolume();
Console.WriteLine("Box1体积:{0}",volume);
volume = Box2.getVolume();
Console.WriteLine("Box2体积:{0}", volume);
Box Box3 = new Box();//box3
Box3 = Box1 + Box2;
volume = Box3.getVolume();
Console.WriteLine("Box3体积:{0}", volume);
Console.ReadKey();
}
}
}