using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace CSharpOperatorOverloadingLab
{
internal class Box
{
private double length;
private double breadth;
private double height;
public void SetLength(double len) { length = len; }
public void SetBreadth(double bre) { breadth = bre; }
public void SetHeight(double hei) { height = hei; }
public double GetVolume()
{
return length * breadth * height;
}
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 next = new Box();
next.length = b.length + 1.0;
next.breadth = b.breadth + 1.0;
next.height = b.height + 1.0;
return next;
}
public static Box operator -(Box b)
{
Box neg = new Box();
neg.length = -b.length;
neg.breadth = -b.breadth;
neg.height = -b.height;
return neg;
}
public static bool operator ==(Box lhs, Box rhs)
{
if (object.ReferenceEquals(lhs, rhs)) return true;
if (object.ReferenceEquals(lhs, null) || object.ReferenceEquals(rhs, null)) return false;
return lhs.length == rhs.length && lhs.breadth == rhs.breadth && lhs.height == rhs.height;
}
public static bool operator !=(Box lhs, Box rhs)
{
return !(lhs == rhs);
}
public static bool operator <(Box lhs, Box rhs)
{
return lhs.length < rhs.length && lhs.breadth < rhs.breadth && lhs.height < rhs.height;
}
public static bool operator >(Box lhs, Box rhs)
{
return lhs.length > rhs.length && lhs.breadth > rhs.breadth && lhs.height > rhs.height;
}
public static bool operator <=(Box lhs, Box rhs)
{
return lhs.length <= rhs.length && lhs.breadth <= rhs.breadth && lhs.height <= rhs.height;
}
public static bool operator >=(Box lhs, Box rhs)
{
return lhs.length >= rhs.length && lhs.breadth >= rhs.breadth && lhs.height >= rhs.height;
}
public override bool Equals(object obj)
{
Box other = obj as Box;
return !object.ReferenceEquals(other, null) && (this == other);
}
public override int GetHashCode()
{
return length.GetHashCode() ^ breadth.GetHashCode() ^ height.GetHashCode();
}
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "({0}, {1}, {2})", length, breadth, height);
}
}
internal class Program
{
private static string Esc(string s)
{
if (s == null) return "null";
StringBuilder sb = new StringBuilder("\"");
foreach (char c in s)
{
switch (c)
{
case '\\': sb.Append("\\\\"); break;
case '"': sb.Append("\\\""); break;
case '\n': sb.Append("\\n"); break;
case '\r': sb.Append("\\r"); break;
case '\t': sb.Append("\\t"); break;
default:
if (c < 32) sb.AppendFormat("\\u{0:x4}", (int)c);
else sb.Append(c);
break;
}
}
sb.Append("\"");
return sb.ToString();
}
private static string CaseJson(string name, string summary, string expected, string actual)
{
bool passed = string.Equals(expected, actual, StringComparison.Ordinal);
return string.Format(
"{{\"name\":{0},\"summary\":{1},\"expected\":{2},\"actual\":{3},\"passed\":{4}}}",
Esc(name), Esc(summary), Esc(expected), Esc(actual), passed ? "true" : "false"
);
}
private static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
List<string> cases = new List<string>();
Box box1 = new Box();
box1.SetLength(6.0); box1.SetBreadth(7.0); box1.SetHeight(5.0);
Box box2 = new Box();
box2.SetLength(12.0); box2.SetBreadth(13.0); box2.SetHeight(10.0);
// Case 1: Binary + overloading & implicit += evaluation
Box box3 = box1 + box2;
Box boxAccum = new Box();
boxAccum.SetLength(1.0); boxAccum.SetBreadth(2.0); boxAccum.SetHeight(3.0);
boxAccum += box1;
string actual1 = string.Format(
CultureInfo.InvariantCulture,
"v1={0},v2={1},b3={2},v3={3},accum={4}",
box1.GetVolume(), box2.GetVolume(), box3.ToString(), box3.GetVolume(), boxAccum.ToString()
);
cases.Add(CaseJson(
"binary_addition_and_compound",
"验证重载二元 + 运算符实现 Box 对象相加,以及 += 自动复用重载后的 + 运算符",
"v1=210,v2=1560,b3=(18, 20, 15),v3=5400,accum=(7, 9, 8)",
actual1
));
// Case 2: Comparison & equality operators
Box box4 = box3;
string actual2 = string.Format(
"gt={0},lt={1},ge={2},le={3},ne={4},eq={5}",
box1 > box2, box1 < box2, box1 >= box2, box1 <= box2, box1 != box2, box3 == box4
);
cases.Add(CaseJson(
"comparison_and_equality_operators",
"验证成对重载的 >、<、>=、<=、==、!= 比较运算符及 ToString 输出",
"gt=False,lt=True,ge=False,le=True,ne=True,eq=True",
actual2
));
// Case 3: Unary operators (++ and unary -)
Box unit = new Box();
unit.SetLength(2.0); unit.SetBreadth(3.0); unit.SetHeight(4.0);
Box incremented = ++unit;
Box negated = -incremented;
string actual3 = string.Format("inc={0},neg={1}", incremented.ToString(), negated.ToString());
cases.Add(CaseJson(
"unary_operator_overloading",
"验证一元运算符 ++ 与一元取负 - 的重载行为",
"inc=(3, 4, 5),neg=(-3, -4, -5)",
actual3
));
Console.WriteLine(string.Format(
"{{\"version\":{0},\"cases\":[{1}]}}",
Esc("CLR " + Environment.Version.ToString()),
string.Join(",", cases.ToArray())
));
}
}
}
本例需在 Windows x64、.NET Framework 4.x Visual C# 编译器(csc.exe)本地离线环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、.NET Framework 4.x Visual C# 编译器(csc.exe)本地离线环境。
入口文件:projects/csharp-operator-overloading-lab/ExploreOperatorOverloading.cs
本地运行步骤快速上手
- 点击上方“保存源码”保存为
ExploreOperatorOverloading.cs,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、.NET Framework 4.x Visual C# 编译器(csc.exe)本地离线环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联42章) · 含当前章节或实例 · mbskycom202612a1321.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1345.zip