qq_58773375 2021-10-09 22:50 采纳率: 100%
浏览 50
已结题

请问为什么会打印地址,这种情况该怎么操作?

class Complex{
int a;
int b;
Complex(int a,int b){
this.a = a;
this.b = b;
}
Complex(){
this.a=0;
this.b=0;
}
Complex add(Complex c){
Complex r = new Complex();
r.a = this.a + c.a;
r.b = this.b + c.b;
return r;

}
Complex sub(Complex d){
    Complex t=new Complex();
    t.a=this.a-d.a;
    t.b=this.b-d.b;
    return d;
}

}
public class TestComplex {

public static void main(String[] args) {
// TODO Auto-generated method stub
Complex c1 = new Complex(40,30);
Complex c2 = new Complex(30,20);
System.out.println(c1.add(c2));

}

}

img

  • 写回答

3条回答 默认 最新

  • CSDN专家-sinJack 2021-10-09 22:58
    关注

    如有帮助,请点击我回答右上角【采纳】按钮
    代码修改如下:

    class Complex{
    int a;
    int b;
    Complex(int a,int b){
    this.a = a;
    this.b = b;
    }
    Complex(){
    this.a=0;
    this.b=0;
    }
    Complex add(Complex c){
    Complex r = new Complex();
    r.a = this.a + c.a;
    r.b = this.b + c.b;
    return r;
    }
    Complex sub(Complex d){
        Complex t=new Complex();
        t.a=this.a-d.a;
        t.b=this.b-d.b;
        return d;
    }
    @Override
    public String toString(){
      return "a="+a+",b="+b;
    }
    }
    public class TestComplex {
    public static void main(String[] args) {
    Complex c1 = new Complex(40,30);
    Complex c2 = new Complex(30,20);
    System.out.println(c1.add(c2));
    }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 10月9日
  • 已采纳回答 10月9日
  • 创建了问题 10月9日

悬赏问题

  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染