永发信息网

C# 如何重载|,&

答案:5  悬赏:70  手机版
解决时间 2021-11-07 13:10
C# 如何重载|,&
最佳答案
顶楼上的

C# 运算符重载决策示例

下面的例子定义一个 Complex 类,实现了复数加、减、乘和除的四则运算。C# 中定义常规运算符的语法如下:

[public | private | protected | internal | internal protected] static | implicit | explicit operator ( )

下面是 C# 3.0 代码。

struct Complex
...{
public double Real ...{ get; set; }
public double Imaginary ...{ get; set; }

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

public static Complex operator +(Complex c1, Complex c2)
...{
return new Complex ...{ Real = c1.Real + c2.Real, Imaginary = c1.Imaginary + c2.Imaginary };
}

public static Complex operator -(Complex c1, Complex c2)
...{
return new Complex ...{ Real = c1.Real - c2.Real, Imaginary = c1.Imaginary - c2.Imaginary };
}

public static Complex operator *(Complex c1, Complex c2)
...{
return new Complex ...{ Real = c1.Real * c2.Real - c1.Imaginary * c2.Imaginary, Imaginary = c1.Real * c2.Imaginary

- c1.Imaginary * c2.Real };
}

public static Complex operator /(Complex c1, Complex c2)
...{
return new Complex ...{ Real = -c1.Real * c2.Real + c1.Imaginary * c2.Imaginary, Imaginary = -c1.Real *

c2.Imaginary + c1.Imaginary * c2.Real };
}
}
由于运算符重载定义在定义它的对象实例上生效,所以可以改写 operator - 和 operator / 运算,使其更加简单。

public static Complex operator -(Complex c1, Complex c2)
...{
return c1 + new Complex ...{ Real = c2.Real, Imaginary = c2.Imaginary };
}

public static Complex operator /(Complex c1, Complex c2)
...{
return c1 * new Complex ...{ Real = -c2.Real, Imaginary = -c2.Imaginary };
}

这样我们就可以很方便的使用 Complex 类:

var c1 = new Complex(3, 4), c2 = new Complex(1, 2);
var c3 = c1 * c2;
Complex c4 = c1 - c2 / c3 + c1;
为了实现更加简单的赋值,我们还需要实现隐式类型转换(从 string 到 Complex)。

public static implicit operator Complex(string value)
...{
value = value.TrimEnd('i');
string[] digits = value.Split('+', '-');
return new Complex ...{ Real = Convert.ToDouble(digits[0]), Imaginary = Convert.ToDouble(digits[1]) };
}

可以通过如下代码对实例进行赋值。

Complex c = "4+5i";
在编译器生成这些运算符重载代码时,实际上会为每个已被重载运算符定义一个特殊名称的方法。如 operator +,其实等同于如下代码:

[SpecialName]
public static Complex op_Addition(Complex c1, Complex c2)
...{
return new Complex ...{ Real = c1.Real + c2.Real, Imaginary = c1.Imaginary + c2.Imaginary };
}
C# 运算符重载一览表

您可以在 C# 中对于以下运算符进行重载决策。

+, -, !, ~, ++, --, true, false
这些一元运算符可被重载。

+, -, *, /, %, &, |, ^, <<, >>
这些二元运算符可被重载。

==, !=, <, >, <=, >=
这些关系运算符可被重载。

&&, ||
这些条件运算符不能被重载,但它们的值被 & 和 | 评估,而这两个运算符可以重载。

[]
数组运算符不能被重载,但您可以定义索引器。

()
转换运算符不能被重载,但您可以定义隐式类型转换和显式类型转换运算符。

+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
这些赋值运算符不能被重载,但他们的值,如 +=,会被 + 评估,而 + 可以被重载。

=, ., ?:, ->, new, is, sizeof, typeof
这些运算符不能被重载。

结论

运算符重载是对重载概念的一个重要补充和发展,它针对对象关系中的多元关系和四则运算、关系运算等常规运算提供了重载支持,开发人员可以利用运算符重载优化利用到这些关系的实现代码中,以提高生产力。
全部回答
楼上几位是不是想分想的看不清楚标题了?

楼主问的是如何操作符重载,不是函数重载,拜托~~

参考这个吧:http://blog.csdn.net/hustorochi/archive/2007/05/13/1607079.aspx
重载
1.方法名称一定要一样。如果不一样,就是两个不同的方法,不能称为重载;
public void Demo();
public void Demo(int age); 重载
2.传入的参数类型一定要不一样。因为电脑需要用参数类型判断调用哪个方法;
public void Demo(int i);
public void Demo(int j); 非法重载
因为参数名称虽然不同,但是类型一样。
看:
public void OK() //
{}
public string OK()
{}
public string OK(string ok)
{}
public string OK(string ok,string name)
{}
public bool ok (string ok)
{}
重载就是同类名不同参 或者是不同的类型都可以.
重载是定义 同一个 类名,但是入参,出参不同;
比如:private void aa(int a,int b)
{ ...}
private void aa(string aa,string bb)
{ ...}
这就是重载。通俗易懂!谢谢!
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
六角龙鱼群养,多长时间喂一次
运动粘度与动力粘度的关系是什么
阅读发榜季栋梁答案
仙剑奇侠传四千佛塔第三层白色的格子怎么走啊
大庆五厂到七厂多远
已知一次函数Y=(3M-8)X+1-M的图象与Y轴的负半
施耐德PLC定义如下全局变量,问在组态王的相
石屏县交警大队违法处理办公室地址在什么地方
怎么加快视屏速度,小米6
这部是啥车?是飞思么?咋标志不一样啊
1000元港币等于多少人民币
进东莞篮球学校要什么条件
单项选择:---Why __________ Jim so sad?---
横溪村地址好找么,我有些事要过去,
用健字起一个微信名字??
推荐资讯
督促函怎么写
金牛座公历和农历是什么座?我5月13日,我不知
我养了一只兔子,我当天出去的时候还活蹦乱跳
世界杯比赛开始时,掷硬币,硬币有什么来历?
工厂上班是不是最低级的工作?
3600元买二手宾得da50-135靠谱吗?会不会是返
丹东有没有一个财专学校,这个学校怎么样
田鼠的原野读后感80字左右
你情愿变得聪明而不冲动吗 但变成步步停下三
重点大学的车辆工程专业大二时一般学哪些课程
谁有火影Narurabu哦?有的进来
128平的房子, 三房两厅两卫,客厅卧室都铺木
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?