我试图为复数创建一个复杂的类。在创建了给出两个复数乘积的mult方法后,它无法运行。在下面的代码中,当我将s定义为:s=mult(u,v(时,eclipse指向一个错误,我不明白为什么。有人能帮忙吗?
package gestion.complexe;
public class Complexe {
double reelle,imag;
public Complexe(double reelle,double imag) {
this.reelle=reelle;
this.imag=imag;
}
public Complexe() {
this(Math.random( )*(2-2),Math.random( )*(2-2));
}
public String toString() {
String s=reelle+" + "+imag+"i";
return s;
}
// Sum of two complex
static Complexe somme(Complexe c1,Complexe c2) {
return new Complexe(c1.reelle+c2.reelle,c1.imag+c2.imag);
}
// Product of two complex numbers
static Complexe mult(Complexe u,Complexe v ) {
return new Complexe(u.reelle*v.reelle-
u.imag*v.imag,u.reelle*v.imag+u.imag*v.reelle);
}
public boolean estReel(Complexe z) {
return z.imag==0;
}
// magnitude of two complex numbers
public double module () {
return Math.sqrt(this.reelle*this.reelle + this.imag*this.imag);
}
}
这是等级测试
package gestion.complexe;
// Test Class
public class Test {
public static void main(String[] args) {
Complexe u=new Complexe(1.0,1.0);
Complexe v=new Complexe(3.0,4.0);
Complexe r=new Complexe(1.0,0.0);
// The problem is the next line, eclipse point error
Complexe s=mult(u,v);
System.out.println(s.toString());
}
}
谢谢你的帮助。
mult
是Complexe
类的静态函数。要使用它,您必须使用类作为前缀,如下所示:
Complexe s = Complexe.mult(u, v);