ylbtech-LanguageSamples-OperatorOverLoading(运算符重载)

ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-OperatorOverLoading(运算符重载)

 

1.A,示例(Sample) 返回顶部

“运算符重载”示例

本示例演示了用户定义的类如何能够重载运算符。有关更多信息,请参见C# 运算符 。

安全说明

提供此代码示例是为了阐释一个概念,它并不代表最安全的编码实践,因此不应在应用程序或网站中使用此代码示例。对于因将此代码示例用于其他用途而出现的偶然或必然的损害,Microsoft 不承担任何责任。

在 Visual Studio 中生成并运行“运算符重载”示例

  1. 在“解决方案资源管理器”中,右击“Complex”项目并单击“设为启动项目”。

  2. 在“调试”菜单上,单击“开始执行(不调试)”。

  3. 对 Dbbool 重复前面这些步骤。

从命令行生成并运行“运算符重载”示例

  1. 使用“更改目录”命令转到“Complex”目录。

  2. 键入以下命令:

    csc complex.cs
    complex
  3. 使用“更改目录”命令转到“Dbbool”目录。

  4. 键入以下命令:

    csc dbbool.cs
    dbbool
1.B,示例代码(Sample Code)返回顶部

1.B.1, complex.cs

// 版权所有(C) Microsoft Corporation。保留所有权利。
// 此代码的发布遵从
// Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。
//
//版权所有(C) Microsoft Corporation。保留所有权利。

// complex.cs
using System;

public struct Complex 
{
   public int real;
   public int imaginary;

   public Complex(int real, int imaginary) 
   {
      this.real = real;
      this.imaginary = imaginary;
   }

   // 声明要重载的运算符 (+)、
   // 可相加的类型(两个 Complex 对象)以及
   // return type (Complex):
   public static Complex operator +(Complex c1, Complex c2) 
   {
      return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
   }
   // 重写 ToString 方法,从而以适当的格式显示复数:
   public override string ToString()
   {
      return(String.Format("{0} + {1}i", real, imaginary));
   }

   public static void Main() 
   {
      Complex num1 = new Complex(2,3);
      Complex num2 = new Complex(3,4);

      // 通过重载的加号运算符
      // 将两个 Complex 对象(num1 和 num2)相加:
      Complex sum = num1 + num2;

     // 使用重写的 ToString 方法打印数字以及相加得到的和:
      Console.WriteLine("First complex number:  {0}",num1);
      Console.WriteLine("Second complex number: {0}",num2);
      Console.WriteLine("The sum of the two numbers: {0}",sum);
 
   }
}
View Code

1.B.2,

1.B.EXE,

First complex number:  2 + 3i
Second complex number: 3 + 4i
The sum of the two numbers: 5 + 7i
请按任意键继续. . .

1.B

1.B,示例代码2(Sample Code)返回顶部

1.B.1, dbbool.cs

// 版权所有(C) Microsoft Corporation。保留所有权利。
// 此代码的发布遵从
// Microsoft 公共许可(MS-PL,http://opensource.org/licenses/ms-pl.html)的条款。
//
//版权所有(C) Microsoft Corporation。保留所有权利。

// dbbool.cs
using System;

public struct DBBool
{
   // 三个可能的 DBBool 值:
   public static readonly DBBool dbNull = new DBBool(0);
   public static readonly DBBool dbFalse = new DBBool(-1);
   public static readonly DBBool dbTrue = new DBBool(1);
   // 为 dbFalse、dbNull、dbTrue 存储 -1、0、1 的私有字段:
   int value; 

   // 私有构造函数。值参数必须为 -1、0 或 1:
   DBBool(int value) 
   {
      this.value = value;
   }

   // 从 bool 到 DBBool 的隐式转换。将 true 映射为
   // DBBool.dbTrue,将 false 映射为 DBBool.dbFalse:
   public static implicit operator DBBool(bool x) 
   {
      return x? dbTrue: dbFalse;
   }

   // 从 DBBool 到 bool 的显式转换。如果
   // 给定的 DBBool 为 dbNull,则引发异常;否则返回
   // true 或 false:
   public static explicit operator bool(DBBool x) 
   {
      if (x.value == 0) throw new InvalidOperationException();
      return x.value > 0;
   }

   // 相等运算符。如果任何一个操作数为 dbNull,则返回 dbNull;
   // 否则返回 dbTrue 或 dbFalse:
   public static DBBool operator ==(DBBool x, DBBool y) 
   {
      if (x.value == 0 || y.value == 0) return dbNull;
      return x.value == y.value? dbTrue: dbFalse;
   }

   // 不等运算符。如果任何一个操作数为
   // dbNull,则返回 dbNull;否则返回 dbTrue 或 dbFalse:
   public static DBBool operator !=(DBBool x, DBBool y) 
   {
      if (x.value == 0 || y.value == 0) return dbNull;
      return x.value != y.value? dbTrue: dbFalse;
   }

   // 逻辑非运算符。如果操作数为
   // dbFalse,则返回 dbTrue;如果操作数为 dbNull,则返回 dbNull;如果
   // 操作数为 dbTrue,则返回 dbFalse:
   public static DBBool operator !(DBBool x) 
   {
      return new DBBool(-x.value);
   }

   // 逻辑“与”运算符。如果任何一个操作数为
   // dbFalse,则返回 dbFalse;如果任何一个操作数为 dbNull,则返回 dbNull;否则,返回 dbTrue:
   public static DBBool operator &(DBBool x, DBBool y) 
   {
      return new DBBool(x.value < y.value? x.value: y.value);
   }

   // 逻辑“或”运算符。如果任何一个操作数为
   // dbTrue,则返回 dbTrue;如果任何一个操作数为 dbNull,则返回 dbNull;否则,返回 dbFalse:
   public static DBBool operator |(DBBool x, DBBool y) 
   {
      return new DBBool(x.value > y.value? x.value: y.value);
   }

   // 绝对真运算符。如果操作数为
   // dbTrue,则返回 true,否则返回 false:
   public static bool operator true(DBBool x) 
   {
      return x.value > 0;
   }

   // 绝对假运算符。如果操作数为
   // dbFalse,则返回 true,否则返回 false:
   public static bool operator false(DBBool x) 
   {
      return x.value < 0;
   }

   // 重载从 DBBool 到 string 的转换:
   public static implicit operator string(DBBool x) 
   {
      return x.value > 0 ? "dbTrue"
           : x.value < 0 ? "dbFalse"
           : "dbNull";
   }

   // 重写 Object.Equals(object o) 方法:
   public override bool Equals(object o) 
   {
      try 
      {
         return (bool) (this == (DBBool) o);
      }
      catch 
      {
         return false;
      }
   }

   // 重写 Object.GetHashCode() 方法:
   public override int GetHashCode() 
   {
      return value;
   }

   // 重写 ToString 方法以便将 DBBool 转换为 string:
   public override string ToString() 
   {
      switch (value) 
      {
         case -1:
            return "DBBool.False";
         case 0:
            return "DBBool.Null";
         case 1:
            return "DBBool.True";
         default:
            throw new InvalidOperationException();
      }
   }
}

class Test 
{
   static void Main() 
   {
      DBBool a, b;
      a = DBBool.dbTrue;
      b = DBBool.dbNull;

      Console.WriteLine( "!{0} = {1}", a, !a);
      Console.WriteLine( "!{0} = {1}", b, !b);
      Console.WriteLine( "{0} & {1} = {2}", a, b, a & b);
      Console.WriteLine( "{0} | {1} = {2}", a, b, a | b);
      // 调用真运算符以确定 DBBool 变量的
      // 布尔值:
      if (b)
         Console.WriteLine("b is definitely true");
      else
         Console.WriteLine("b is not definitely true");   
   }
}
View Code

1.B.2,

1.B.EXE,

!DBBool.True = DBBool.False
!DBBool.Null = DBBool.Null
DBBool.True & DBBool.Null = DBBool.Null
DBBool.True | DBBool.Null = DBBool.True
b is not definitely true
请按任意键继续. . .

1.B,

1.C,下载地址(Free Download)返回顶部
warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/ylbtech/p/4197192.html