如何使+操作员的工作量,同时增加两分彼此? [英] How to make the + operator work while adding two Points to each other?
问题描述
有没有办法让 +
运营工作,为Point对象?
Is there any way to get the +
operator to work for the Point object?
举个例子来说,这个小片段:
Take, for example, this tiny snippet:
this.cm1.Show((MouseEventArgs)e.Location+this.i_rendered.Location);
您看,我尽量补充两点海誓山盟。它只是不工作(这是预期)。我很想得到这个工作。
You see, I try to add two points to eachother. It just does not work (which was expected). I'd love to get this working.
任何想法?
推荐答案
这不会发生,你期望的方式。唯一的过载,该点
结构提供了 +
(加)运算符的one是翻译的点
由<$ C坐标$ C>尺寸 。
It's not going to happen the way you expect. The only overload that the Point
structure provides for the +
(addition) operator is one that translates the coordinates of the Point
by a Size
.
有没有办法将两个点
结构在一起,我甚至不知道这意味着什么。
There's no way to add two Point
structures together, and I'm not even sure what that would mean.
不要浪费太多时间想出来的,或者说,考虑到<一href="http://stackoverflow.com/questions/172658/operator-overloading-with-c-sharp-extension-methods">you不能写超负荷运营商的扩展方法。
Don't waste too much time figuring it out, either, considering that you cannot write extension methods that overload operators.
幸运的是,在编译语言,没有处罚分手了code成多行。所以,你可以重新写你的code如下:
Fortunately, in a compiled language, there's no penalty for splitting up code into multiple lines. So you can re-write your code as follows:
Point newLocation = new Point(e.Location.X + this.i_rendered.Location.X,
e.Location.Y + this.i_rendered.Location.Y);
this.cm1.Show(newLocation);
另外,您也可以使用 偏移
方法一>,但我不认为这增强了可读性。
Alternatively, you could use the Offset
method, but I'm not convinced that enhances readability.
这篇关于如何使+操作员的工作量,同时增加两分彼此?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!