一、题目描述
运用面向对象的抽象和封装理论对数学中的复数进行抽象,类名为公有的Complex,该类对象应该有双精度类型的实部和虚部成员,应提供至少2个公有构造方法和相应的get方法和set方法,还应该有公有的equals方法和toString方法,应提供用于加减乘除(add()、sub()、muti()、div())计算的成员方法和类方法。
二、思路
1.因为需要运用封装的思想,所以所有的成员变量都需要变成私有。
2.两个构造方法的设计
3.自动生成get和set方法,equals方法
4.写成toString方法
5.实现方法时,需要实现类方法和成员方法,而且要考虑传入的形参和返回值的类型问题
三、代码
package project;
public class Complex {
//设置成员变量的实部和虚部
private double real, imag;
//无参构造方法
public Complex() {
//在内部同时调用带参构造
this(0,0);
}
//带参构造方法
public Complex(double real, double imag) {
super();
this.real = real;
this.imag = imag;
}
/**
* @return the real 返回实部
*/
public double getReal() {
return real;
}
/**
* @param real the real to set 设置实部
*/
public void setReal(double real) {
this.real = real;
}
/**
* @return the imag 返回虚部
*/
public double getImag() {
return imag;
}
/**
* @param imag the imag to set 设置虚部
*/
public void setImag(double imag) {
this.imag = imag;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode() 设置hashCode方法
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(imag);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(real);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object) 设置equals方法
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof Complex)) {
return false;
}
Complex other = (Complex) obj;
if (Double.doubleToLongBits(imag) != Double.doubleToLongBits(other.imag)) {
return false;
}
if (Double.doubleToLongBits(real) != Double.doubleToLongBits(other.real)) {
return false;
}
return true;
}
/* (non-Javadoc)
* @see java.lang.Object#toString() toString方法返回“实部+虚部i”的形式
*/
@Override
public String toString() {
return ""+real+"+"+imag+"i";
}
/**
* 实现add成员方法
* @param Complex another 传入的是Complex类
* @return Complex 返回的是Complex类
*/
public Complex add(Complex another) {
//内层是直接在传入参数的时候进行实部和虚部的相加
return new Complex(this.real+another.real,this.imag+another.imag);
}
/**
* 实现add的类方法
* @param Complex a, Complex b 传入的是两个Complex类
* @return Complex 返回的是Complex类
*/
public static Complex add(Complex a,Complex b) {
//内层是直接在传入参数的时候进行实部和虚部的相加
return new Complex(a.real+b.real,a.imag+b.imag);
}
/**
* 实现sub的成员方法
* @param Complex another 传入的是Complex类
* @return Complex 返回的是Complex类
*/
public Complex sub(Complex another) {
//内层是直接在传入参数的时候进行实部和虚部的相减
return new Complex(real-another.real,imag-another.imag);
}
/**
* 实现sub的类方法
* @param Complex a, Complex b 传入的是两个Complex类
* @return Complex 返回的是Complex类
*/
public static Complex sub(Complex a,Complex b) {
//内层是直接在传入参数的时候进行实部和虚部的相减
return new Complex(a.real-b.real,a.imag-b.imag);
}
/**
* 实现muti的成员方法
* @param Complex another 传入的是Complex类
* @return Complex 返回的是Complex类
*/
public Complex muti(Complex another) {
//内层是直接在传入参数的时候进行实部和虚部的相乘
return new Complex(real*another.real-imag*another.imag,imag*another.real+real*another.imag);
}
/**
* 实现muti的类方法
* @param Complex a, Complex b 传入的是两个Complex类
* @return Complex 返回的是Complex类
*/
public static Complex muti(Complex a,Complex b) {
//内层是直接在传入参数的时候进行实部和虚部的相乘
return new Complex(a.real*b.real-a.imag*b.imag,a.imag*b.real+a.real*b.imag);
}
/**
* 实现div的类方法
* @param Complex a, Complex b 传入的是两个Complex类
* @return Complex 返回的是Complex类
*/
public static Complex div(Complex a,Complex b) {
//设置复数除法的分子和分母
double x=a.real*b.real+a.imag*b.imag;
double y=a.imag*b.real-a.real*b.imag;
double z=b.real*b.real+b.imag*b.imag;
//接在传入参数的时候进行实部和虚部的相除
return new Complex(x/z,y/z);
}
/**
* 实现div的成员方法
* @param Complex another 传入的是Complex类
* @return Complex 返回的是Complex类
*/
public Complex div(Complex another) {
//直接调用类方法完成计算
return Complex.div(this, another);
}
}