c# 运算符重载 方法

来源:百度知道 编辑:UC知道 时间:2024/06/07 15:47:42
public static TwoD operator +(TwoD op1, TwoD op2)
{
TwoD result = new TwoD();
result.x = op1.x + op2.x;
result.y = op1.y + op2.y;
return result;
}
public static TwoD operator +(TwoD op1, int op2)
{
TwoD result = new TwoD();
result.x = op1.x + op2;
result.y = op1.y + op2;
return result;
}

public override string ToString()
{
return string.Format("x坐标:{0},y坐标{1}",x,y);
}

关于类的实例化。。怎么在实例化中输入。2个参数实现坐标+法呢?

你的是静态函数,不可以实例化。
你要实例化,把static关键字去掉!

class Oper{
public int X{set;get};
public int Y{set;get};
public Oper(int argX,arg Y){X=argX;Y=argY;}
public GetAddResult(){return X+Y;}
}
这样?
最好把坐标提出来,写个包含X和Y的数据结构,储存起来。你这样很乱的。