初学者问个简单的问题,望英雄指点,里面说

bzwm 2005-10-26 09:22:45
我想编写一个复数的类,至少实现服输相加,相减,相乘(这里我只做到了相加,就出错误了,没有继续)!
根据一点C++的基础,写出了这个类,可是在写toString()这个方法的时候总是出错!
代码如下:
public class Complex {

public static void main(String[] args) {
Complex x = new Complex(1,2);
Complex y = new Complex(2,2);
Complex z = new Complex(0,0);
z.Add(x,y);
z.toString();
}
private int real,imag;
public Complex(int re,int im)
{
real = re;
imag = im;
}
public void Set(int re,int im)
{
real = re;
imag = im;
}
public int GetR()
{
return real;
}
public int GetI()
{
return imag;
}
public Complex Add(Complex x1,Complex x2)
{
Complex x3;
int temp1,temp2;
temp1 = x1.real + x2.real;
temp2 = x1.imag + x2.imag;
x3 = new Complex(temp1,temp2);
return x3;
}
public void toString()
{

System.out.print(real);

}

}

提示出错信息:toString() in Complex cannot override toString() in java .lang.Object;
attempting to use incompatible return type
...全文
113 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
bzwm 2005-10-27
  • 打赏
  • 举报
回复
谢谢 水与争锋和老怪了!
现在明白了
犯了不少错误啊!学到了很多东西!
zhouyong80 2005-10-27
  • 打赏
  • 举报
回复
public class CompplexNumber{
//实例变量,每个复数对象有两个双精度值x和y
//他们是private的,因此不能在类以外访问,但
//他们可以通过real()和imaginary()方法得到
private double x,y;

/**初始化x和y的构造函数*/
public ComplexNumber(double real,double imaginary){
this.x = real;
this.y = imaginary;
}

/**
* 返回复数实部的存取器方法
* 主义没有用于设置实部的setReal()方法
* 这意味着ComplexNumber类是不可变的
**/
public double real(){ return x;}

/**返回复数虚部的存取器方法 */
public double imaginary() { return y;}

/** 计算复数的幅值 */
public double magnitude(){
return Math.sqrt(x*x+y*y);
}

/** 这个方法将ComplexNumber转换为一个字符串
* 它覆盖了Object的方法
**/
public String toString(){
return "{" + x + "," + y + "}";
}

/**
* 静态方法,接收两个复数,相加,返回他们的和
* 因为是静态的,所以没有"当前实例"或"this"对象
* 其用法是:ComplexNumber c = ComplexNumber.add(a,b);
**/
public static ComplexNumber add(ComplexNumber a,ComplexNumber b){
return new ComplexNumber(a.x + b.x ,a,y + b.y);
}

/**
* 同名的非静态方法
* 将指定的数与当前数相加,用法是:
* ComplexNumber c=a.add(b);
**/
public ComplexNumber add(ComplexNumber a){
return new ComplexNumber(this.x + a.x,this.y + a.y);
}

/**复数相乘的静态方法 */
public static ComplexNumber multiply(ComplexNumber a,ComplexNumber b){
return new ComplexNumber(a.x * b.x -a.y * b.y,a.x * b.y + a.y * b.x);
}

/**复数相乘的实例方法 */
public ComplexNumber multiply(ComplexNumber a){
return new ComplexNumber(x * a.x - y * a.y,x * a.y + y * a.x);
}

}
紫炎圣骑 2005-10-26
  • 打赏
  • 举报
回复
package test1;

public class Complex {

public static void main(String[] args) {
Complex x = new Complex(5,2);
Complex y = new Complex(2,2);
Complex z = new Complex(0,0);
z = Add(x,y);
System.out.println(z);
}
private int real,imag;
public Complex(int re,int im)
{
real = re;
imag = im;
}
public void Set(int re,int im)
{
real = re;
imag = im;
}
public int GetR()
{
return real;
}
public int GetI()
{
return imag;
}
public static Complex Add(Complex x1,Complex x2)
{
Complex x3;
int temp1,temp2;
temp1 = x1.real + x2.real;
temp2 = x1.imag + x2.imag;
x3 = new Complex(temp1,temp2);
return x3;
}
public String toString()
{
return Integer.toString(real);
}

}
低调的小青蛙 2005-10-26
  • 打赏
  • 举报
回复
顺便说,按照你的 Add 的写法,

z.Add(x,y);

是不能够将 x,y 的和给 z 的。


最好这样设计:

z = Complex.Add(x, y);

//(static) 表示2个数相加
public static Complex Add(Complex x1, Complex x2)
{
return new Complex(x1.real + x2.real, x1.imag + x2.imag);
}

//(非static)表示将另一个x1添加给当前实例
public void Add(Complex x1)
{
this.real += x1.real;
this.imag += x1.imag;
}
低调的小青蛙 2005-10-26
  • 打赏
  • 举报
回复
错在这里:

public void toString()
————————————

toString() 是 Object 类的一个方法,也就是说,所有的类都有这个方法。toString 需要返回一个字符串。

你可以写
public String toString()
{
return new StringBuffer(real).toString();
}

然后在 main 中写
System.out.println(z);

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧