一个C#基础问题 请高手指教 谢谢
程序1 using System;
using System.Collections.Generic;
using System.Text;
namespace 操作符重载2
{
class Complex
{
double r, v; //r+ v i
public Complex(double r, double v)
{
this.r = r;
this.v = v;
}
public static Complex operator +(Complex a, Complex b)
{
return new Complex(a.r + b.r, a.v + b.v);
}
public static Complex operator -(Complex a)
{
return new Complex(-a.r, -a.v);
}
public static Complex operator ++(Complex a)
{
double r = a.r + 1;
double v = a.v + 1;
return new Complex(r, v);
}
public void Print()
{
Console.Write(r + " + " + v + "i");
}
}
class Test
{
public static void Main()
{
Complex a = new Complex(3, 4);
Complex b = new Complex(5, 6);
Complex c = -a;
c.Print();
Complex d = a + b;
d.Print();
a.Print();
Complex e = a++;
a.Print();
e.Print();
Complex f = ++a;
a.Print();
f.Print();
}
}
}
程序2;
using System;
class Player
{
public int neili;
public int tili;
public int jingyan;
public int neili_r;
public int tili_r;
public Player()
{
neili=10;
tili=50;
jingyan=0;
neli_r=50;
tili_r=50;
}
public static Player operator ++(Player p){
p.neili=p.neili+50;
p.tili=p.tili+100;
p.neili_r=p.neili;
p.tili_r=p.tili;
return p;
}
public void Show()
{
Console.WriteLine("Tili:{0}",tili);
Console.WriteLine("Neili:{0}",neili);
Console.WriteLine("Tili_full:{0}",tili_r);
Console.WriteLine("Neili_full:{0}",neili_r);
}
}
class Test
{
public static void Main(){
Player man=new Player();
man.Show();
man++;
Console.WriteLine("Now upgrading...:");
man.Show();
}
}
问:程序1中 Complex c = -a;
c.print();
程序2中 man++;
为什么1中要用实例c,而2中却可以不用呢 2可直接用man实例引用