}
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 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; } 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; } public override string ToString() { return String.Format("{0}, {1}, {2}", length, breadth, height); } } class Tester { static void Main(string[] args) { Box box1 = new Box(); Box box2 = new Box(); Box box3 = new Box(); double volume = 0.0; // Box1 详述 box1.setLength(6.0); box1.setBreadth(7.0); box1.setHeight(5.0); // Box2 详述 box2.setLength(12.0); box2.setBreadth(13.0); box2.setHeight(10.0); // Box1 的体积 volume = box1.getVolume(); Console.WriteLine("Box1 的体积: {0}", volume); // Box2 的体积 volume = box2.getVolume(); Console.WriteLine("Box2 的体积: {0}", volume); box3 = box2 + box1; Console.WriteLine("Box3: {0}",box3.ToString()); volume = box3.getVolume(); Console.WriteLine("Box3的体积:{0}", volume); Console.ReadLine(); } } }
程序运行结果如下,(供参考)
