永发信息网

C# 如何重载|,&

答案:5  悬赏:70  手机版
解决时间 2021-11-07 13:10
  • 提问者网友:姑娘长的好罪过
  • 2021-11-07 07:09
C# 如何重载|,&
最佳答案
  • 五星知识达人网友:迟山
  • 2021-11-07 07:28
顶楼上的

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
这些运算符不能被重载。

结论

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

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

参考这个吧:http://blog.csdn.net/hustorochi/archive/2007/05/13/1607079.aspx
  • 2楼网友:夜余生
  • 2021-11-07 09:36
重载
1.方法名称一定要一样。如果不一样,就是两个不同的方法,不能称为重载;
public void Demo();
public void Demo(int age); 重载
2.传入的参数类型一定要不一样。因为电脑需要用参数类型判断调用哪个方法;
public void Demo(int i);
public void Demo(int j); 非法重载
因为参数名称虽然不同,但是类型一样。
  • 3楼网友:忘川信使
  • 2021-11-07 08:43
看:
public void OK() //
{}
public string OK()
{}
public string OK(string ok)
{}
public string OK(string ok,string name)
{}
public bool ok (string ok)
{}
重载就是同类名不同参 或者是不同的类型都可以.
  • 4楼网友:迷人又混蛋
  • 2021-11-07 08:32
重载是定义 同一个 类名,但是入参,出参不同;
比如:private void aa(int a,int b)
{ ...}
private void aa(string aa,string bb)
{ ...}
这就是重载。通俗易懂!谢谢!
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯