我想编写一个复数的类,至少实现服输相加,相减,相乘(这里我只做到了相加,就出错误了,没有继续)!
根据一点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
根据一点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
解决方案 »
- 求教:类集、集合类以及容器的区别是什么?
- 大家来说说什么软件做做出来的uml图最漂亮?
- JSP的一种实现
- HTML標簽問題,急!
- 创建一个单列线程的问题
- 求助:通过combo1通给方法参数,通过button1获取JTextArea in的值
- 请教一下java中怎么分隔字段,怎样实现顺序读取啊?
- 在创建自己的异常类时出错,请赐教!
- jbuilder8->Tools->configure libraries是配置什么的?
- java中有没有destructor的东东,好像一般的class里都没有嘛,难道不要手工做这些事情,全部交给gc处理吗?
- 大家帮忙看看这段List程序,多谢了!
- 用jasperreport+ireport输出Excel表格的问题!
————————————toString() 是 Object 类的一个方法,也就是说,所有的类都有这个方法。toString 需要返回一个字符串。你可以写
public String toString()
{
return new StringBuffer(real).toString();
}然后在 main 中写
System.out.println(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;
}
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);
}}
//实例变量,每个复数对象有两个双精度值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);
}
}