无法在测试类中调用方法。

huangapple go评论75阅读模式
英文:

Failing to call a method in test class

问题

这是你提供的代码的翻译:

我试图创建一个处理复数的复数类在创建了表示两个复数乘积的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;
    }
    // 两个复数的和
    static Complexe somme(Complexe c1, Complexe c2) {
        return new Complexe(c1.reelle + c2.reelle, c1.imag + c2.imag);

    }
    // 两个复数的乘积
    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;
    }
    // 两个复数的模
    public double module() {
        return Math.sqrt(this.reelle * this.reelle + this.imag * this.imag);
    }
}

这是测试类

package gestion.complexe;
// 测试类
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);
        // 问题出在下一行,Eclipse显示错误
        Complexe s = mult(u, v);
        System.out.println(s.toString());

    }

}
感谢帮助
英文:

I was trying to make a complex class for complex numbers. After creating the mult method giving the product of two complex numbers it fails to run. In this code below, when i define s as: s=mult(u,v), eclipse point an error and I don't understand why. Can anyone give a help?

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);
}
}

here is the class test

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());
}
}

thanks for helping.

答案1

得分: 1

multComplexe类的静态函数。要使用它,您必须像这样使用该类作为前缀:

Complexe s = Complexe.mult(u, v);
英文:

mult is a static function of the Complexe class. To use it, you have to use the class as a prefix like this:

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

huangapple
  • 本文由 发表于 2020年9月22日 01:05:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63996929.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定