C#演示用户定义的类如何能够重载运算符的代码

2019-01-11  本文已影响0人  gougoude

将写内容过程比较常用的内容片段备份一下,下面资料是关于C#演示用户定义的类如何能够重载运算符的内容。

using System;

public struct Complex

{

  public int real;

  public int imaginary;

  public Complex(int real, int imaginary)

  {

      this.real = real;

      this.imaginary = imaginary;

  }

  public static Complex operator +(Complex c1, Complex c2)

  {

      return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);

  }

  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 sum = num1 + num2;

      Console.WriteLine("First complex number:  {0}",num1);

      Console.WriteLine("Second complex number: {0}",num2);

      Console.WriteLine("The sum of the two numbers: {0}",sum);

  }

}

代码2

using System;

public struct 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);

  int value;

  DBBool(int value)

  {

      this.value = value;

  }

  public static implicit operator DBBool(bool x)

  {

      return x? dbTrue: dbFalse;

  }

  public static explicit operator bool(DBBool x)

  {

      if (x.value == 0) throw new InvalidOperationException();

      return x.value > 0;

  }

  public static DBBool operator ==(DBBool x, DBBool y)

  {

      if (x.value == 0 || y.value == 0) return dbNull;

      return x.value == y.value? 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;

  }

  public static DBBool operator !(DBBool x)

  {

      return new DBBool(-x.value);

  }

  public static DBBool operator &(DBBool x, DBBool y)

  {

      return new DBBool(x.value < y.value? x.value: y.value);

  }

  public static DBBool operator |(DBBool x, DBBool y)

  {

      return new DBBool(x.value > y.value? x.value: y.value);

  }

  public static bool operator true(DBBool x)

  {

      return x.value > 0;

  }

  public static bool operator false(DBBool x)

  {

      return x.value < 0;

  }

  public static implicit operator string(DBBool x)

  {

      return x.value > 0 ? "dbTrue"

          : x.value < 0 ? "dbFalse"

          : "dbNull";

  }

  public override bool Equals(object o)

  {

      try

      {

        return (bool) (this == (DBBool) o);

      }

      catch

      {

        return false;

      }

  }

  public override int GetHashCode()

  {

      return value;

  }

  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);

      if (b)

        Console.WriteLine("b is definitely true");

      else

        Console.WriteLine("b is not definitely true"); 

  }

}

上一篇下一篇

猜你喜欢

热点阅读