主题:运算符重载及域的用法
public class Complex
{
public double real;
public double image;
public double Real
{
get
{
return real;
}
}
public double Image
{
get
{
return image;
}
}
public Complex()
{
this.real=0;
this.image =1;
}
public Complex(double real,double image)
{
this.real =real;
this.image =image;
}
public static double ABSComplex(Complex c)
{
return Math.Sqrt (c.Real*c.Real +c.Image*c.Image);
}
public static Complex operator+(Complex c1,Complex c2)
{
return new Complex (c1.Real +c2.Real ,c1.Image +c2.Image );
}
public static Complex operator-(Complex c1,Complex c2)
{
return new Complex (c1.Real -c2.Real ,c1.Image -c2.Image );
}
public static Complex operator*(Complex c1,Complex c2)
{
double r=c1.Real *c2.Real -c1.Image *c2.Image;
double i=c1.Image *c2.Real +c1.Real *c2.Image ;
return new Complex(r,i);
}
public static Complex operator/(Complex c1,Complex c2)
{
double div=ABSComplex(c2);
double r=(c1.Real*c2.Real+c1.Image*c2.Image)/div/div;
double i=(c1.Image*c2.Real -c1.Real *c2.Image )/div/div;
return new Complex (r,i);
}
public override string ToString()//将复数转化成字符串,所有自定义的C#类都隐含地从Object类派生这里是重载Object类的虚构方法
{
string str;
if(this.image >0)
{
str=this.real.ToString ()+"+"+this.image .ToString ()+"i";
}
else
{
str=this.real .ToString ()+this.image .ToString ()+"i";
}
return str;
}
}
public class T
{
public static void Main()
{
Complex t1=new Complex (1,1);
Complex t2=new Complex (1,1);
Console.WriteLine (t1);
Console.WriteLine (t2);
Console.WriteLine (t1+t2);
Console.WriteLine (t1-t2);
Console.WriteLine (t1*t2);
Console.WriteLine (t1/t2);
}
}
域都有哪些用处?
在什么情况下用到域
{
public double real;
public double image;
public double Real
{
get
{
return real;
}
}
public double Image
{
get
{
return image;
}
}
public Complex()
{
this.real=0;
this.image =1;
}
public Complex(double real,double image)
{
this.real =real;
this.image =image;
}
public static double ABSComplex(Complex c)
{
return Math.Sqrt (c.Real*c.Real +c.Image*c.Image);
}
public static Complex operator+(Complex c1,Complex c2)
{
return new Complex (c1.Real +c2.Real ,c1.Image +c2.Image );
}
public static Complex operator-(Complex c1,Complex c2)
{
return new Complex (c1.Real -c2.Real ,c1.Image -c2.Image );
}
public static Complex operator*(Complex c1,Complex c2)
{
double r=c1.Real *c2.Real -c1.Image *c2.Image;
double i=c1.Image *c2.Real +c1.Real *c2.Image ;
return new Complex(r,i);
}
public static Complex operator/(Complex c1,Complex c2)
{
double div=ABSComplex(c2);
double r=(c1.Real*c2.Real+c1.Image*c2.Image)/div/div;
double i=(c1.Image*c2.Real -c1.Real *c2.Image )/div/div;
return new Complex (r,i);
}
public override string ToString()//将复数转化成字符串,所有自定义的C#类都隐含地从Object类派生这里是重载Object类的虚构方法
{
string str;
if(this.image >0)
{
str=this.real.ToString ()+"+"+this.image .ToString ()+"i";
}
else
{
str=this.real .ToString ()+this.image .ToString ()+"i";
}
return str;
}
}
public class T
{
public static void Main()
{
Complex t1=new Complex (1,1);
Complex t2=new Complex (1,1);
Console.WriteLine (t1);
Console.WriteLine (t2);
Console.WriteLine (t1+t2);
Console.WriteLine (t1-t2);
Console.WriteLine (t1*t2);
Console.WriteLine (t1/t2);
}
}
域都有哪些用处?
在什么情况下用到域