首页 > 解决方案 > 未能在测试类中调用方法

问题描述

我试图为复数创建一个复杂的类。在创建给出两个复数乘积的 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());

}

}

谢谢你的帮助。

标签: java

解决方案


multComplexe类的静态函数。要使用它,您必须使用类作为前缀,如下所示:

Complexe s = Complexe.mult(u, v);

推荐阅读