zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C#运算符重载

c# 运算符 重载
2023-09-11 14:16:46 时间

一 运算符

1 使用运算符的例子

① this.Location+=new Size(10,10);
② TimeSpan diff=date2-date1;
③ String s1,s2;…if(s1==s2);

2 运算符有时比方法名更直观

如 两个复数用 a+b 比a.Add(b)更直观;
但要慎用;

3 运算符重载有一些限制

① 如成对,如类型要求,如有的不能重载;

二 运算符的声明

1 一元运算符声明的形式如下:

public static 类型 operator 一元运算符(类型 参数名){…}

2 二元运算符声明的形式如下:

public static 类型 operator 二元运算符(类型 参数名,类型 参数名){…};

3 类型转换运算符声明的形式如下:

public static implicit operator 类型(类型 参数名){…}
public static explicit operator 类型(类型 参数名)
{…}

复数的运算符重载

using System;
using System.Collections.Generic;
using System.Diagnostics.SymbolStore;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;

namespace 复数的运算符重载
{
    public struct Complex
    {
        public double real;
        public double imaginary;

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

        public static Complex operator+(Complex c1)
        {
            return c1;
        }

        public static Complex operator-(Complex c1)
        {
            return new Complex(-c1.real, -c1.imaginary);
        }
    
        public static bool operator true(Complex c1)
        {
            return c1.real != 0 || c1.imaginary != 0;
        }

        public static bool operator false(Complex c1)
        {
            return c1.real == 0 && c1.imaginary == 0;
        }

        public static Complex operator+(Complex c1,Complex c2)
        {
            return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
        }

        public static Complex operator-(Complex c1,Complex c2)
        {
            return c1 + (-c2);
        }

        public static Complex operator*(Complex c1,Complex c2)
        {
            return new Complex(c1.real * c2.real - c1.imaginary * c2.imaginary,
                c1.real * c2.imaginary + c1.imaginary * c2.real);
        }

        public static Complex operator*(Complex c,double k)
        {
            return new Complex(c.real * k, c.imaginary * k);
        }

        public static Complex operator*(double k,Complex c)
        {
            return c * k;
        }

        public override string ToString()
        {
            return (System.String.Format("{0}+{1}i)", real, imaginary));
        }

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

            Complex result = num1 ? -num1 * 5 + num1 * num2 : new Complex(0, 0);
            System.Console.WriteLine("First complex number:{0}", num1);
            System.Console.WriteLine("Second Complex number:{0}", num2);
            System.Console.WriteLine("The result is:{0}", result);
            Console.ReadKey();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*

Digit类型定义了下面的操作符:

·   从Digit到byte的隐式转换操作符。

·   从byte到Digit的隐式转换操作符

·   把两个Digit数值相加并返回一个Digit数值的加法操作符。

·   把一共Digit数值与其它Digit数值相减,并返回一共digit数值的减法操作符。

·   比较两个digit数值的等式和非等式。

*/

namespace 全面的重载
{
   public struct Digit
    {
        byte value;
        public Digit(byte value)
        {
            if (value < 0 || value > 0) throw new ArgumentException();
            this.value = value;
        }

        public Digit(int value) : this((byte)value) { }

        public static implicit operator byte(Digit d)
        {
            return d.value;
        }

        public static explicit operator Digit(byte b)
        {
            return new Digit(b);
        }

        public static Digit operator+(Digit a,Digit b)
        {
            return new Digit(a.value + b.value);
        }

        public static Digit operator-(Digit a,Digit b)
        {
            return new Digit(a.value - b.value);
        }

        public static bool operator==(Digit a,Digit b)
        {
            return a.value == b.value;
        }

        public static bool operator!=(Digit a,Digit b)
        {
            return a.value != b.value;
        }

        public override bool Equals(object obj)
        {
            return this == (Digit)value;
        }

        public override int GetHashCode()
        {
            return value.GetHashCode();
        }

        public override string ToString()
        {
            return value.ToString();
        }
    }

    class Test
    {
        static void Main()
        {
            Digit a = (Digit)5;
            Digit b = (Digit)3;
            Digit plus = a + b;
            Digit minus = a - b;
            bool equals = (a == b);

            Console.WriteLine("{0}+{1}={2}", a, b, plus);
            Console.WriteLine("{0}-{1}={2}", a, b, minus);
            Console.WriteLine("{0}=={1}=={2}", a, b, equals);
            Console.ReadKey();
        }
    }
}