永发信息网

【跪求】【Java】编程实现一个复数类Complex,要求其实部和虚部用private的成员变量表达

答案:4  悬赏:60  手机版
解决时间 2021-03-06 02:00
  • 提问者网友:战皆罪
  • 2021-03-05 02:06
【跪求】【Java】编程实现一个复数类Complex,要求其实部和虚部用private的成员变量表达
最佳答案
  • 五星知识达人网友:长青诗
  • 2021-03-05 03:06
public Complex{
    private double real;
    private double image;
    
    public Complex(){
        this(0d,0d);
    }
    public Complex(double r,double i){
        real=r;image=i;
    }
    public Complex(Complex ref){
        this(ref.getReal(),ref.getImage());
    }
    
    public double getReal(){
        return real;
    }
    public double getImage(){
        return image;
    }
}

不知道能不能满足你的需要!
全部回答
  • 1楼网友:逃夭
  • 2021-03-05 05:56
package cn.miw.x;
public class Complex {
private double r, i;
public Complex() {
super();
r = 0;
i = 0;
}
public Complex(double r, double l) {
super();
this.r = r;
this.i = l;
}
public Complex(Complex ref) {
super();
r = ref.r;
i = ref.i;
}
public double getReal() {
return r;
}
public double getImage() {
return i;
}
}
这个应该很简单嘛 望采纳。
  • 2楼网友:拾荒鲤
  • 2021-03-05 04:17
public class Complex
{
private double image;
private double real;
public Complex()
{
this.image=0;
this.real=0;
}
public Complex(double r,double i)
{
this.image=i;
this.real=r;
}
public Complex(Complex ref)
{
ref.image=this.image;
ref.real=this.real;
}
public double getReal()
{
return this.real;
}
public double getImage()
{
return this.image;
}
public String toString()
{
if(this.image<0)
{
return "("+this.real+"-"+this.image+"i)";
}
else
return "("+this.real+"+"+this.image+"i)";
}
public static void main(String[] args)
{
Complex ox=new Complex(4.0,1.0);
System.out.println("real="+ox.getReal());
System.out.println("Image="+ox.getImage());
Complex ox1=new Complex(5.0,6.0);
System.out.println(ox1);
System.out.println(ox);
}
}
  • 3楼网友:千杯敬自由
  • 2021-03-05 03:29
public class Complex{
private double real = 0;
private double image = 0;
public Comples(){
real =0;
image=0;
}
public Complex(double real,double image){
this.real=real;
this.image=image;
}
public double getReal(){
return real;
}
public double getImage(){
return image;
}
public void setReal(double real){
this.real=real;
}
public void setImage(double image){
this.image=image;
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯