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

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

作者: gougoude | 来源:发表于2019-01-11 11:37 被阅读0次

将写内容过程比较常用的内容片段备份一下,下面资料是关于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"); 

  }

}

相关文章

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

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

  • C# 第三节

    C# 运算符重载 您可以重定义或重载 C# 中内置的运算符。因此,程序员也可以使用用户自定义类型的运算符。重载运算...

  • C++运算符重载4

    重载函数运算符 演示代码1: 演示代码2:

  • C++运算符重载初步

    说明 此处为MyCircle类重载了一系列运算符,按照半径进行比较,用以演示关系运算符重载用法。 示例代码

  • 运算符重载与友元函数

    运算符重载 C++允许将运算符重载到用户定义的类型,例如,使用+将两个类对象相加。 重载运算符要使用运算符函数: ...

  • python 运算符的重载

    什么是运算符重载: 让自定义的类生成的对象(实例)能够使用运算符进行操作 运算符重载的作用: 让自定义的实例像内建...

  • C#重载运算符

    C#中比较运算符的重载 == and != ; > and <; >= and <=. c#中要求比较运算符的重载...

  • 运算符重载

    对于内置的数据类,编译器直到如何进行运算 对于自定义数据类型,需要运算符重载 加号运算符重载 左移运算符重载 重载...

  • 面向对象程序设计-运算符重载(2020-04-06)

    当运算符作用于类类型的运算对象时,可以通过运算符重载重新定义该运算符的含义,合适的使用运算符重载可以提高代码的可读...

  • 10-25学习总结

    今天继续学习了C#第四章 1.运算符重载 运算符也是C#类的一个重要成员,系统对大部分运算符都给出了常规定义,这些...

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/byztdqtx.html