public static AddClass1 operator -(AddClass1 op1) { AddClass1 returnVal = new AddClass1(); returnVal.Val = -op1.Val; return(returnVal); }
//重载+ public static AddClass1 operator +(AddClass1 op1, AddClass1 op2) { AddClass1 returnVal = new AddClass1(); returnVal.Val = op1.Val + op2.Val; return(returnVal); }
static void Main(string[] args) { AddClass1 op1 = new AddClass1(); op1.Val = 1; AddClass1 op2 = new AddClass1(); op2.Val = 2; AddClass1 op3 = op1 + op2; Console.WriteLine("op3.Var={0}", op3.Val); AddClass2 op4 = new AddClass2(); op4.Val = 10; AddClass3 op5 = op1 + op4; Console.WriteLine("op5.Var={0}", op5.Val); Console.ReadKey(); }