C# 如何使用*
net的XNA框架有一个非常有用的对象vector2,它表示一个二维向量。你可以用int、float和其他向量2乘以它们 例如 唯一的一点是,X,Y信息存储为float,在很多情况下,我只想将2d信息或2d坐标存储为int。 我想为这个做我自己的结构。。 这是我的C# 如何使用*,c#,.net,struct,multiplication,C#,.net,Struct,Multiplication,net的XNA框架有一个非常有用的对象vector2,它表示一个二维向量。你可以用int、float和其他向量2乘以它们 例如 唯一的一点是,X,Y信息存储为float,在很多情况下,我只想将2d信息或2d坐标存储为int。 我想为这个做我自己的结构。。 这是我的 internal struct Coord { public int X { get; private set; } public int Y { get; private set; } public Coo
internal struct Coord
{
public int X { get; private set; }
public int Y { get; private set; }
public Coord(int x,int y)
{
X = x;
Y = y;
}
}
我的问题是如何使我的Coord结构可以使用*而不是乘法函数调用乘以int或其他Coord您可以使用运算符重载:
public static Coord operator*(Coord left, int right)
{
return new Coord(left.X * right, left.Y * right);
}
只需将该方法放入Coord结构中。您可以使用许多运算符(如+、-、/等)执行此操作。。。也可以使用不同的参数。您可以使用运算符重载:
public static Coord operator*(Coord left, int right)
{
return new Coord(left.X * right, left.Y * right);
}
只需将该方法放入Coord结构中。您可以使用许多运算符(如+、-、/等)执行此操作。。。还有不同的参数。您需要为您的键入重载
// overload operator *
public static Coord operator *(Coord x, Coord y)
{
// Return a `Coord` that results from multiplying x with y
}
您需要为您键入的内容重载
// overload operator *
public static Coord operator *(Coord x, Coord y)
{
// Return a `Coord` that results from multiplying x with y
}
重载乘法运算符:
public static Coord operator* (Coord multiplyThis, Coord withThis) {
Coord result = multiply(multiplyThis, withThis); //...multiply the 2 Coords
return result;
}
public static Coord operator* (Coord multiplyThis, float withThis) {
Coord result = multiply(multiplyThis, withThis); //...multiply the Coord with the float
return result;
}
重载乘法运算符:
public static Coord operator* (Coord multiplyThis, Coord withThis) {
Coord result = multiply(multiplyThis, withThis); //...multiply the 2 Coords
return result;
}
public static Coord operator* (Coord multiplyThis, float withThis) {
Coord result = multiply(multiplyThis, withThis); //...multiply the Coord with the float
return result;
}