C# 重载加号运算符计算两个对象的和
public class ValidFloat { private float value; private bool valid; public ValidFloat (float value, bool valid) { this.value = value; this.valid = valid; } public static ValidFloat operator+ (ValidFloat vf1, ValidFloat vf2) { return new ValidFloat (vf1.value + vf2.value, vf1.valid & vf2.valid); } public static void Main( ) { ValidFloat vf1 = new ValidFloat ( 1.1f, true); ValidFloat vf2 = new ValidFloat ( 2.2f, false); ValidFloat vf3 = vf1 + vf2; Console.WriteLine ("Value = " + vf3.value + " Valid: " + vf3.valid); } }