zoukankan      html  css  js  c++  java
  • C#之运算符重载

      我们知道,对于一些基本的数据类型(Int32、long、double等)我们可以通过+、-,*等进行一些运算,但是对于自己定义的一些复杂类型,如果我们想实现这些功能只能通过定义一些方法,以执行这些操作,这样肯定不太直观。如果要想自己的类上使用这些运算符功能,就必须告诉编译器相关的运算符在这个类中的含义以及运算逻辑,此时就用到了运算符重载。

      运算符的重载不仅限于算术运算符,在进行重载时还需要考虑到一些比较运算符==、<、>、!=、>=、<=等。例如if(a==b),对于类,在默认情况下这个语句是比较这两个对象的引用的是否指向同一个地址,而不是去比较这两个对象的字段值是否相等;另外,对于结构,默认情况下==运算符是不进行工作的,如果试图比较两个结构看它们是否相等,就会产生一个编译错误,所以我们要比较结构,就必须显示的重载==运算符,告诉编译器如何进行比较。下面,简单的运用code进行一些介绍

    一、算术运算符+、-、*、/重载

    View Code
    namespace ConsoleApp
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                Point p1 = new Point(10, 8);
                Point p2 = new Point(5, 4);
    
                Point p3 = p1 + p2;
                Point p4 = p1 - p2;
                Point p5 = p1 * p2;
                Point p6 = p1 / p2;
    
                Console.WriteLine("p1—" + p1);
                Console.WriteLine("p2—" + p2);
                Console.WriteLine("p3—" + p3);
                Console.WriteLine("p4—" + p4);
                Console.WriteLine("p5—" + p5);
                Console.WriteLine("p6—" + p6);
            }
        }
    
        public class Point
        {
            private Int32 x;
            private Int32 y;
    
            public Point()
            {
                this.x = 0;
                this.y = 0;
            }
            public Point(Int32 _x, Int32 _y)
            {
                this.x = _x;
                this.y = _y;
            }
    
            public Int32 X
            {
                get { return x; }
                set { x = value; }
            }
    
            public Int32 Y
            {
                get { return y; }
                set { y = value; }
            }
    
            /// <summary>
            /// 重载+运算符
            /// </summary>
            public static Point operator +(Point p1, Point p2)
            {
                return new Point(p1.x + p2.x, p1.y + p2.y);
            }
    
            /// <summary>
            /// 重载-运算符
            /// </summary>
            public static Point operator -(Point p1, Point p2)
            {
                return new Point(p1.x - p2.x, p1.y - p2.y);
            }
    
            /// <summary>
            /// 重载*运算符
            /// </summary>
            public static Point operator *(Point p1, Point p2)
            {
                return new Point(p1.x * p2.x, p1.y * p2.y);
            }
    
            /// <summary>
            /// 重载/运算符
            /// </summary>
            public static Point operator /(Point p1, Point p2)
            {
                if (p2.x == 0 || p2.y == 0)
                    throw new Exception("被除数中不能有0的数");
    
                return new Point(p1.x / p2.x, p1.y / p2.y);
            }
    
            public override string ToString()
            {
                return String.Format("X:{0},Y:{1}", this.x, this.y);
            }
        }
    }

     二、比较运算符重载

    View Code
    namespace ConsoleApp
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                Point p1 = new Point(10, 8);
                Point p2 = new Point(5, 4);
                Point p3 = new Point(5, 4);
    
                Console.WriteLine("P1 > P2:" + (p1 > p2).ToString());
                Console.WriteLine("P2 < P1:" + (p2 < p1).ToString());
                Console.WriteLine("P2 == P3:" + (p2 == p3).ToString());
                
            }
        }
    
        public class Point
        {
            private Int32 x;
            private Int32 y;
    
            public Point()
            {
                this.x = 0;
                this.y = 0;
            }
            public Point(Int32 _x, Int32 _y)
            {
                this.x = _x;
                this.y = _y;
            }
    
            public Int32 X
            {
                get { return x; }
                set { x = value; }
            }
    
            public Int32 Y
            {
                get { return y; }
                set { y = value; }
            }
    
            #region 比较运算符重载
    
            /// <summary>
            /// 重载相等运算符
            /// </summary>
            public static Boolean operator ==(Point p1, Point p2)
            {
                return (p1.x == p2.x) && (p1.y == p2.y);
            }
    
            /// <summary>
            /// 重载不相等运算符
            /// </summary>
            public static Boolean operator !=(Point p1, Point p2)
            {
                return !(p1.x == p2.x) && (p1.y == p2.y);
            }
    
            /// <summary>
            /// 重载>运算符
            /// </summary>
            public static Boolean operator >(Point p1, Point p2)
            {
                return (p1.x * p1.x + p1.y * p1.y) > (p2.x * p2.x + p2.y * p2.y);
            }
    
            /// <summary>
            /// 重载<运算符
            /// </summary>
            public static Boolean operator <(Point p1, Point p2)
            {
                return (p1.x * p1.x + p1.y * p1.y) < (p2.x * p2.x + p2.y * p2.y);
            }
    
            /// <summary>
            /// 重载>=运算符
            /// </summary>
            public static Boolean operator >=(Point p1, Point p2)
            {
                return (p1.x * p1.x + p1.y * p1.y) >= (p2.x * p2.x + p2.y * p2.y);
            }
    
            /// <summary>
            /// 重载<=运算符
            /// </summary>
            public static Boolean operator <=(Point p1, Point p2)
            {
                return (p1.x * p1.x + p1.y * p1.y) <= (p2.x * p2.x + p2.y * p2.y);
            }
    
            #endregion
    
            public override string ToString()
            {
                return String.Format("X:{0},Y:{1}", this.x, this.y);
            }
            
        }
    }
  • 相关阅读:
    将自己的工作环境全面移植到C++最后一道工序:能用MFC制作简单的图形界面
    Finally, the working environment has been moved to C++
    统计方面的书籍【zz】
    zz sql 通配符以及转义字符用法
    转载学习并实现DES加密解密算法(三)
    【资源分享】2009版大陆汉语常用字.txt下载
    自己实现的C++Trim()
    nmake命令(windows下的makefile)
    c++对象内存模型【内存布局】
    UML类图关系(VPUML工具绘图)
  • 原文地址:https://www.cnblogs.com/NaughtyBoy/p/2540113.html
Copyright © 2011-2022 走看看