嘿嘿sorry啊!声明并测试一个复数类,其方法包括toString()及复数的加,减,乘运算。
追答package test;
public class Fushu {
int shibu;
int xubu;
public Fushu(){}
public Fushu(int shibu, int xubu){
this.shibu = shibu;
this.xubu = xubu;
}
public String toString(){
return this.shibu+"+"+this.xubu+"i";
}
/**
* 加法
* @param x
* @param y
* @return
*/
public static Fushu add(Fushu x,Fushu y){
Fushu fushu = new Fushu();
fushu.shibu = x.shibu + y.shibu;
fushu.xubu = x.xubu + y.xubu;
return fushu;
}
/**
* 减法
* @param x
* @param y
* @return
*/
public static Fushu sub(Fushu x,Fushu y){
Fushu fushu = new Fushu();
fushu.shibu = x.shibu - y.shibu;
fushu.xubu = x.xubu - y.xubu;
return fushu;
}
/**
* 乘法
* @param x
* @param y
* @return
*/
public static Fushu mul(Fushu x,Fushu y){
Fushu fushu = new Fushu();
fushu.shibu = x.shibu * y.shibu - x.xubu * y.xubu;
fushu.xubu = x.shibu * y.xubu + y.shibu * x.xubu;
return fushu;
}
}