欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详细分析c# 运算符重载

程序员文章站 2022-08-30 12:18:24
您可以重定义或重载 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# 运算符重载的资料请关注其它相关文章!