运算符重载问题
假设我有一个类:
public class Vector
{
public float X { get; set; }
public Vector(float xvalue)
{
X = xvalue;
}
public static Vector operator +(Vector v1, Vector v2)
{
return new Vector(v1.X + v2.X);
}
}
有一个派生类:
public class MyVector : Vector
{
public static MyVector operator +(MyVector v1, MyVector v2)
{
return new MyVector(v1.X + v2.X);
}
public MyVector(float xvalue):base(xvalue)
{
}
}
如果我执行以下代码,则不会:
Vector v1 = new MyVector(10); //type MyVector
Vector v2 = new MyVector(20); //type MyVector
Vector v3 = v1 + v2; //executed operator is of Vector class
这里执行的是 Vector 的 + 运算符,但 v1 和 v2 的类型是 MyVector
,所以此时 v3 是 Vector 类型。
为什么会出现这种情况?
Suppose I have a class:
public class Vector
{
public float X { get; set; }
public Vector(float xvalue)
{
X = xvalue;
}
public static Vector operator +(Vector v1, Vector v2)
{
return new Vector(v1.X + v2.X);
}
}
Have a derived class:
public class MyVector : Vector
{
public static MyVector operator +(MyVector v1, MyVector v2)
{
return new MyVector(v1.X + v2.X);
}
public MyVector(float xvalue):base(xvalue)
{
}
}
No if I execute folowing code:
Vector v1 = new MyVector(10); //type MyVector
Vector v2 = new MyVector(20); //type MyVector
Vector v3 = v1 + v2; //executed operator is of Vector class
Here is executed Vector's + operator, but the type of v1 and v2 is MyVector
, so v3 is Vector type at this point.
Why this happens?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为变量
v1
和v2
的类型是Vector
,而不是MyVector
。运算符重载是编译器在编译时而不是运行时解析的静态方法;它们不能被覆盖。v1
和v2
必须输入MyVector
,编译器才能选择MyVector
类中定义的重载。(可选)在
Vector
类上定义一个方法public virtual Vector Add(Vector other)
并让MyVector
重写它。然后从Vector
的operator+
方法调用此方法,这将按您的预期工作。 (MyVector
将不需要定义operator+
本身。)Because the type of the variables
v1
andv2
areVector
, notMyVector
. Operator overloads are static methods that are resolved by the compiler at compile-time, not at runtime; they cannot be overridden.v1
andv2
will have to be typedMyVector
for the compiler to select the overload defined in theMyVector
class.Optionally, define a method
public virtual Vector Add(Vector other)
on theVector
class and haveMyVector
override it. Then call this method from theoperator+
method ofVector
and this will work as you expected. (MyVector
would then not need to defineoperator+
itself.)就像 cdhowie 回答 一样,方法解析是在编译类型完成的。
但如果您使用 C# 4.0,则可以尝试以下操作。
可以看到,在
bar()
方法中,方法调用解析将在运行时完成,使用v1
和v2
的 true 类型,而不是编译时感知的类型(如foo()
中)。:-)
PS:在运行时延迟类型解析可能会更慢,而且不太安全(因为应该在编译时捕获的错误将在执行期间被发现)。
Like cdhowie answered, the method resolution is done at compile type.
But if you're working with C# 4.0, you can try the following.
To see that in the
bar()
method, the method call resolution will be done at runtime, using the true type ofv1
andv2
, instead of the perceived type at compile time (as infoo()
).:-)
P.S.: Delaying type resolution at runtime can be slower, and less safe (as errors that should have been caught at compile time will be discovered during execution).