C# 中的运算符重载

发布于 2024-11-06 16:12:24 字数 595 浏览 0 评论 0原文

class Point
{
    private int m_PointX;
    private int m_PointY;

    public Point(int x, int y)
    {
        m_PointX = x;
        m_PointY = y;
    }

    public static Point operator+(Point point1, Point point2)
    {
        Point P = new Point();
        P.X = point1.X + point2.X;
        P.Y = point1.Y + point2.Y;

        return P;
    }
}

示例:

Point P1 = new Point(10,20);
Point P2 = new Point(30,40)
P1+P2; // operator overloading
  1. 是否有必要始终将运算符重载函数声明为静态?这背后的原因是什么?
  2. 如果我想重载 + 来接受像 2+P2 这样的表达式,该怎么做?
class Point
{
    private int m_PointX;
    private int m_PointY;

    public Point(int x, int y)
    {
        m_PointX = x;
        m_PointY = y;
    }

    public static Point operator+(Point point1, Point point2)
    {
        Point P = new Point();
        P.X = point1.X + point2.X;
        P.Y = point1.Y + point2.Y;

        return P;
    }
}

Example:

Point P1 = new Point(10,20);
Point P2 = new Point(30,40)
P1+P2; // operator overloading
  1. Is it necessary to always declare the operator overloading function as static? What is the reason behind this?
  2. If I want to overload + to accept the expression like 2+P2, how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苏佲洛 2024-11-13 16:12:24
  1. 是的。因为您并不总是与运算符一起处理实例。
  2. 只需将类型更改为您想要的即可。

这是#2 的示例,

public static Point operator+(int value, Point point2)
{
 // logic here.
}

如果您希望 P2 + 2 工作,则必须以其他方式处理参数。

请参阅 http://msdn.microsoft.com/en-us/library/8edha89s。 aspx 了解更多信息。

  1. Yes. Because you aren't dealing with instances always with the operators.
  2. Just change the types to what you want.

Here is an example for #2

public static Point operator+(int value, Point point2)
{
 // logic here.
}

You will have to do the other way with the parameters if you want P2 + 2 to work.

See http://msdn.microsoft.com/en-us/library/8edha89s.aspx for more information.

弥繁 2024-11-13 16:12:24

回答您的问题:

  1. 是的,您需要将它们定义为静态。它们不是实例方法,它们也可以对 null 进行操作。
  2. 您必须定义一个运算符重载,其中参数之一的类型为 int

To answer your questions:

  1. Yes, you need to define them as static. They're not instance methods, they can operate on nulls as well.
  2. You'll have to define an operator overload where one of the parameters are of type int
顾北清歌寒 2024-11-13 16:12:24

前面的两个答案都讨论了你的问题,所以我不会打扰这些问题,但这里有一个使用 2+P 的示例:

   public static Point operator+(int yourInt, Point point)
    {
        Point P = new Point();
        P.X = point.X + yourInt;
        P.Y = point.Y + yourInt;

        return P;
    }

Both of the previous answers talk about your questions, so I'm not going to intrude on those, but here is an example of using 2+P:

   public static Point operator+(int yourInt, Point point)
    {
        Point P = new Point();
        P.X = point.X + yourInt;
        P.Y = point.Y + yourInt;

        return P;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文