C#中运算符重载的例子

下边举了个运算符重载的例子

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

class Point
{
    
    
    public int x;
    public int y;

    public static Point operator +(Point p1,Point p2)
    {
    
    
        Point p = new Point();
        p.x = p1.x + p2.x;
        p.y = p1.y + p2.y;
        return p;
    }
}


public class TestTwo : MonoBehaviour
{
    
    
    // Start is called before the first frame update
    void Start()
    {
    
    
        Point p = new Point();
        p.x = 1;
        p.y = 1;
        Point p2 = new Point();
        p2.x = 2;
        p2.y = 3;

        Point p3 = p + p2;
    }

}

猜你喜欢

转载自blog.csdn.net/charlsdm/article/details/125051316