这是一个OperatorOverride项目里的默认文件的程序,(需要调用下面自己创建的重载类文件Number)
using System;
using System.Collections.Generic;using System.Linq;using System.Text;namespace OperatorOverride
{ class Program { static void Main(string[] args) { int tempCount = 10; Number num = new Number(); Number sum = new Number(); Number sub = new Number();Console.WriteLine("原始数字为:-->{0}",num.count);
sum = sum + tempCount; sub = sum - tempCount; Console.WriteLine("加上10之后的数字为-->{0}", sum.count); Console.WriteLine("减去10之后的数字为-->{0}",sub.count); Console.ReadLine(); } }}这个是类文件Number
using System;
using System.Collections.Generic;using System.Linq;using System.Text;namespace OperatorOverride
{ class Number { public int count; public Number() { count = 20; } public static Number operator +(Number n, int num) { int tempCount = n.count + num; Number num1 = new Number(); num1.count = tempCount; return num1;}
public static Number operator -(Number n, int num) { int tempCount = n.count - num; Number num1 = new Number(); num1.count = tempCount; return num1; } }}