C#运算符的重载 - 图1

    1. using System;
    2. public struct Point
    3. {
    4. public double x;
    5. public double y;
    6. public Point(double x, double y)
    7. {
    8. this.x = x;
    9. this.y = y;
    10. }
    11. // 运算符的重载,使加号具有新的功能
    12. // 可以实现两个Point对象直接相加,得到一个新的点
    13. public static Point operator +(Point p1, Point p2)
    14. {
    15. return new Point(p1.x + p2.x, p1.y + p2.y);
    16. }
    17. }
    18. public class Program
    19. {
    20. public static void Main(string[] args)
    21. {
    22. Point p1 = new Point(1, 2);
    23. Point p2 = new Point(2, 3);
    24. Point p3 = p1 + p2;
    25. Console.WriteLine(p3.x);
    26. Console.WriteLine(p3.y);
    27. }
    28. }