回 帖 发 新 帖 刷新版面

主题:这个程序怎么会出现这样的结果???

using Systerm;
namespace Calculator
{  
    class Calculator
    {
       public int number1,number2;
       public Calculator( int num1,int num2)
       {
           number1=num1;
           number2=num2;
        }
        public static Calculator operator -(Calculator c1)
       {
            c1.number1=-c1.number1;
            c1.number2=-c1.number2;
             return c1;
        }
         public void Print()
        {
            Console.WriteLine(" number1=" +number1);
            Console.WriteLine(" number2=" +number2);
            Console.ReadLine();
         }
     }
         class EnterPoint
        {
               static void Main()
                 {
                    Calculator calc = new Calculator(15,-25);
                     calc=-calc;
                     calc.Print();
                  }
        }
}

输出
number1=-15
number2=25

这是关于运算符重载的问题    我一直没有弄的懂   拜托帮我解释一下这个程序的运行过程[color=FF0000]-15   25[/color]

回复列表 (共1个回复)

沙发

关键是这名          
c1.number1=-c1.number1;
c1.number2=-c1.number2;
当然是相反

我来回复

您尚未登录,请登录后再回复。点此登录或注册