Plural class
Comprising a plurality of generating, addition and subtraction, and the return portion and an imaginary portion fact
package lei;
public class fushu1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Complex a=new Complex(2,3);
Complex b=new Complex(4,5);
Complex c=a.Add(b);
Complex d=b.Add(a);
System.out.println("复数a="+a.tosting());
System.out.println("复数b="+b.tosting());
System.out.println("a的实部为"+a.getreal());
System.out.println("a的虚部为"+a.getimg());
System.out.println("复数c="+c.tosting());
System.out.println("复数d="+d.tosting());
}
}
class Complex
{
private double real; //private表示私有,其他不能访问或改变,需写一个返回函数
private double img;
Complex()
{
real=0;
img=0;
}
Complex(double n1,double n2)
{
real=n1;
img=n2;
}
String tosting()
{
return real+"+"+img+"i";
}
Complex Add(Complex b)
{
Complex c=new Complex();
c.real=this.real+b.real;
c.img=this.img+b.img;
return c;
}
double getreal() //返回其私有值,在主函数中调用
{
return real;
}
double getimg()
{
return img;
}
}