当前位置: 移动技术网 > IT编程>开发语言>c# > 详细分析c# 运算符重载

详细分析c# 运算符重载

2020年08月17日  | 移动技术网IT编程  | 我要评论
您可以重定义或重载 c# 中内置的运算符。因此,程序员也可以使用用户自定义类型的运算符。重载运算符是具有特殊名称的函数,是通过关键字 operator 后跟运算符的符号来定义的。与其他函数一样,重载运

您可以重定义或重载 c# 中内置的运算符。因此,程序员也可以使用用户自定义类型的运算符。重载运算符是具有特殊名称的函数,是通过关键字 operator 后跟运算符的符号来定义的。与其他函数一样,重载运算符有返回类型和参数列表。

例如,请看下面的函数:

public static box operator+ (box b, box c)
{
  box box = new box();
  box.length = b.length + c.length;
  box.breadth = b.breadth + c.breadth;
  box.height = b.height + c.height;
  return box;
}

上面的函数为用户自定义的类 box 实现了加法运算符(+)。它把两个 box 对象的属性相加,并返回相加后的 box 对象。

运算符重载的实现

下面的程序演示了完整的实现:

using system;

namespace operatorovlapplication
{
  class box
  {
   private double length;   // 长度
   private double breadth;   // 宽度
   private double height;   // 高度

   public double getvolume()
   {
     return length * breadth * height;
   }
   public void setlength( double len )
   {
     length = len;
   }

   public void setbreadth( double bre )
   {
     breadth = bre;
   }

   public void setheight( double hei )
   {
     height = hei;
   }
   // 重载 + 运算符来把两个 box 对象相加
   public static box operator+ (box b, box c)
   {
     box box = new box();
     box.length = b.length + c.length;
     box.breadth = b.breadth + c.breadth;
     box.height = b.height + c.height;
     return box;
   }

  }

  class tester
  {
   static void main(string[] args)
   {
     box box1 = new box();     // 声明 box1,类型为 box
     box box2 = new box();     // 声明 box2,类型为 box
     box box3 = new box();     // 声明 box3,类型为 box
     double volume = 0.0;     // 体积

     // box1 详述
     box1.setlength(6.0);
     box1.setbreadth(7.0);
     box1.setheight(5.0);

     // box2 详述
     box2.setlength(12.0);
     box2.setbreadth(13.0);
     box2.setheight(10.0);

     // box1 的体积
     volume = box1.getvolume();
     console.writeline("box1 的体积: {0}", volume);

     // box2 的体积
     volume = box2.getvolume();
     console.writeline("box2 的体积: {0}", volume);

     // 把两个对象相加
     box3 = box1 + box2;

     // box3 的体积
     volume = box3.getvolume();
     console.writeline("box3 的体积: {0}", volume);
     console.readkey();
   }
  }
}

当上面的代码被编译和执行时,它会产生下列结果:

box1 的体积: 210
box2 的体积: 1560
box3 的体积: 5400

可重载和不可重载运算符

下表描述了 c# 中运算符重载的能力:

运算符 描述
+, -, !, ~, ++, -- 这些一元运算符只有一个操作数,且可以被重载。
+, -, *, /, % 这些二元运算符带有两个操作数,且可以被重载。
==, !=, <, >, <=, >= 这些比较运算符可以被重载。
&&, || 这些条件逻辑运算符不能被直接重载。
+=, -=, *=, /=, %= 这些赋值运算符不能被重载。
=, ., ?:, ->, new, is, sizeof, typeof 这些运算符不能被重载。

实例

针对上述讨论,让我们扩展上面的实例,重载更多的运算符:

using system;

namespace operatorovlapplication
{
  class box
  {
    private double length;   // 长度
    private double breadth;   // 宽度
    private double height;   // 高度
   
    public double getvolume()
    {
     return length * breadth * height;
    }
   public void setlength( double len )
   {
     length = len;
   }

   public void setbreadth( double bre )
   {
     breadth = bre;
   }

   public void setheight( double hei )
   {
     height = hei;
   }
   // 重载 + 运算符来把两个 box 对象相加
   public static box operator+ (box b, box c)
   {
     box box = new box();
     box.length = b.length + c.length;
     box.breadth = b.breadth + c.breadth;
     box.height = b.height + c.height;
     return box;
   }
   
   public static bool operator == (box lhs, box rhs)
   {
     bool status = false;
     if (lhs.length == rhs.length && lhs.height == rhs.height
       && lhs.breadth == rhs.breadth)
     {
       status = true;
     }
     return status;
   }
   public static bool operator !=(box lhs, box rhs)
   {
     bool status = false;
     if (lhs.length != rhs.length || lhs.height != rhs.height
       || lhs.breadth != rhs.breadth)
     {
       status = true;
     }
     return status;
   }
   public static bool operator <(box lhs, box rhs)
   {
     bool status = false;
     if (lhs.length < rhs.length && lhs.height
       < rhs.height && lhs.breadth < rhs.breadth)
     {
       status = true;
     }
     return status;
   }

   public static bool operator >(box lhs, box rhs)
   {
     bool status = false;
     if (lhs.length > rhs.length && lhs.height
       > rhs.height && lhs.breadth > rhs.breadth)
     {
       status = true;
     }
     return status;
   }

   public static bool operator <=(box lhs, box rhs)
   {
     bool status = false;
     if (lhs.length <= rhs.length && lhs.height
       <= rhs.height && lhs.breadth <= rhs.breadth)
     {
       status = true;
     }
     return status;
   }

   public static bool operator >=(box lhs, box rhs)
   {
     bool status = false;
     if (lhs.length >= rhs.length && lhs.height
       >= rhs.height && lhs.breadth >= rhs.breadth)
     {
       status = true;
     }
     return status;
   }
   public override string tostring()
   {
     return string.format("({0}, {1}, {2})", length, breadth, height);
   }
  
  }
  
  class tester
  {
   static void main(string[] args)
   {
    box box1 = new box();     // 声明 box1,类型为 box
    box box2 = new box();     // 声明 box2,类型为 box
    box box3 = new box();     // 声明 box3,类型为 box
    box box4 = new box();
    double volume = 0.0;  // 体积

    // box1 详述
    box1.setlength(6.0);
    box1.setbreadth(7.0);
    box1.setheight(5.0);

    // box2 详述
    box2.setlength(12.0);
    box2.setbreadth(13.0);
    box2.setheight(10.0);

    // 使用重载的 tostring() 显示两个盒子
    console.writeline("box1: {0}", box1.tostring());
    console.writeline("box2: {0}", box2.tostring());
    
    // box1 的体积
    volume = box1.getvolume();
    console.writeline("box1 的体积: {0}", volume);

    // box2 的体积
    volume = box2.getvolume();
    console.writeline("box2 的体积: {0}", volume);

    // 把两个对象相加
    box3 = box1 + box2;
    console.writeline("box3: {0}", box3.tostring());
    // box3 的体积
    volume = box3.getvolume();
    console.writeline("box3 的体积: {0}", volume);

    //comparing the boxes
    if (box1 > box2)
     console.writeline("box1 大于 box2");
    else
     console.writeline("box1 不大于 box2");
    if (box1 < box2)
     console.writeline("box1 小于 box2");
    else
     console.writeline("box1 不小于 box2");
    if (box1 >= box2)
     console.writeline("box1 大于等于 box2");
    else
     console.writeline("box1 不大于等于 box2");
    if (box1 <= box2)
     console.writeline("box1 小于等于 box2");
    else
     console.writeline("box1 不小于等于 box2");
    if (box1 != box2)
     console.writeline("box1 不等于 box2");
    else
     console.writeline("box1 等于 box2");
    box4 = box3;
    if (box3 == box4)
     console.writeline("box3 等于 box4");
    else
     console.writeline("box3 不等于 box4");

    console.readkey();
   }
  }
}

当上面的代码被编译和执行时,它会产生下列结果:

box1: (6, 7, 5)
box2: (12, 13, 10)
box1 的体积: 210
box2 的体积: 1560
box3: (18, 20, 15)
box3 的体积: 5400
box1 不大于 box2
box1 小于 box2
box1 不大于等于 box2
box1 小于等于 box2
box1 不等于 box2
box3 等于 box4

以上就是详细分析c# 运算符重载的详细内容,更多关于c# 运算符重载的资料请关注移动技术网其它相关文章!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

  • c# 面试必备线程基础知识点

    c# 面试必备线程基础知识点

    线程的知识太多,知识点有深有浅,往深的研究会涉及操作系统、cpu、内存,往浅了说就是一些语法。没有一定的知识积累,很难把线程的知识写得全面,当然我也没有这个能力... [阅读全文]
  • C#使用System.Net邮件发送功能踩过的坑

    C#使用System.Net邮件发送功能踩过的坑

    1.eazyemail邮件发送类库net 类库自带了邮件发送功能。笔者对该类库,从使用的角度进行了二次封装,nuget上可搜索eazyemail,注入容器时通过... [阅读全文]
  • C#基于Modbus三种CRC16校验方法的性能对比

    C#基于Modbus三种CRC16校验方法的性能对比

    1.背景介绍主要应用场景在物联网中,底端设备注册报文的上报,需要对报文的有效载荷(data)进行crc16的复验,验证与设备端的crc校验是否相等,如果相等,报... [阅读全文]
  • 深入谈谈C#9新特性的实际运用

    前言你一定会好奇:“老周,你去哪开飞机了?这么久没写博客了。”老周:“我买不起飞机,开了个铁矿,挖了一年半的石头。谁知铁矿垮了,压死了几条蜈蚣,什么也没挖着。”... [阅读全文]
  • C# 泛型集合的自定义类型排序的实现

    C# 泛型集合的自定义类型排序的实现

    一、泛型集合list<t>排序经sort方法之后,采用了升序的方式进行排列的。二、对自定义类型进行排序定义一个普通类:接下来,将定义的person实... [阅读全文]
  • C#开发中常用的加密解密方法汇总

    相信很多人在开发过程中经常会遇到需要对一些重要的信息进行加密处理,今天给大家分享我个人总结的一些加密算法:常见的加密方式分为可逆和不可逆两种方式可逆:rsa,a... [阅读全文]
  • C# 如何添加错误日志信息

    系统日志系统日志包含了由windows系统组件记录的事件。例如,在启动期间装入驱动程序或其他系统组件失败被记录到系统日志。要查看系统日志: 打开命令提示符。 ... [阅读全文]
  • 关于C#委托三种调用的分享使用

    关于C#委托三种调用的分享使用

    一、同步调用1、同步调用会按照代码顺序来执行2、同步调用会阻塞线程,如果是要调用一项繁重的工作(如大量io操作),可能会让程序停顿很长时间,造成糟糕的用户体验,... [阅读全文]
  • 用c# 自动更新程序

    作者:冰封一夏出处:hzhcontrols官网:首先看获取和更新的接口更新程序program.cs更新程序界面定义服务端接口,你可以用任意接口都行,我这里用we... [阅读全文]
  • c# 生成二维码的示例

    二维码是越来越流行了,很多地方都有可能是使用到。如果是静态的二维码还是比较好处理的,通过在线工具就可以直接生成一张二维码图片,比如:草料二维码。但有的时候是需要... [阅读全文]
验证码:
移动技术网