关于c#复式的一个程序

来源:百度知道 编辑:UC知道 时间:2024/09/23 10:29:54
这个是类:class Complex
{
private double real;

public double Real
{
get { return real; }
set { real = value; }
}
private double imaginary;

public double Imaginary
{
get { return imaginary; }
set { imaginary = value; }
}

public Complex(double real, double imaginary)
{
this.real = real;
this.imaginary = imaginary;
}
public static Complex operator +(Complex a, Complex b)
{
return new Complex(a.real + b.real, a.imaginary + b.imaginary);
}
public static Complex operator ++(Complex a)
{
return new Complex(a.real + 1, a.imaginary + 1);
}

public void show()
{
Console.WriteL

运算符重载,通俗点说比如有类叫人,有两个属性姓名和年龄,如果我们规定当两个人相加的时候就是将两个人的年龄进行相加,这个时候我们就要用到运算符重载,来规定“+”的含义,不然编译器是不知道怎么将两个人进行相加运算的
public static Complex operator +(Complex a, Complex b)
{
return new Complex(a.real + b.real, a.imaginary + b.imaginary);
}
这里 就是定义“+”的含义,当我们使用a+b运算的时候会调用
Complex(a.real + b.real, a.imaginary + b.imaginary)
这个函数将两个对象的real属性和imaginary属性分别进行相加