java面向对象的题目,求这道题的解题思路,感激不尽!!
2个回答
展开全部
public class Complex {
int realPart;
int imaginPart;
/**
* Constructor
*/
public Complex() {
super();
}
/**
* @param realPart
* @param imaginPart
*/
public Complex(int realPart, int imaginPart) {
super();
this.realPart = realPart;
this.imaginPart = imaginPart;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return realPart + "+" + imaginPart + "i";
}
public Complex complexAdd(Complex a) {
int r = this.getRealPart() + a.getRealPart();
int i = this.getImaginPart() + a.getImaginPart();
return new Complex(r, i);
}
/**
* @return the realPart
*/
public int getRealPart() {
return realPart;
}
/**
* @param realPart
* the realPart to set
*/
public void setRealPart(int realPart) {
this.realPart = realPart;
}
/**
* @return the imaginPart
*/
public int getImaginPart() {
return imaginPart;
}
/**
* @param imaginPart
* the imaginPart to set
*/
public void setImaginPart(int imaginPart) {
this.imaginPart = imaginPart;
}
public static void main(String[] args) {
// 创建两个对象a,b
Complex a = new Complex(1, 2);
Complex b = new Complex(3, 4);
// 相加得到c
Complex c = a.complexAdd(b);
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
直接把上面复制,运行
给分吧
int realPart;
int imaginPart;
/**
* Constructor
*/
public Complex() {
super();
}
/**
* @param realPart
* @param imaginPart
*/
public Complex(int realPart, int imaginPart) {
super();
this.realPart = realPart;
this.imaginPart = imaginPart;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return realPart + "+" + imaginPart + "i";
}
public Complex complexAdd(Complex a) {
int r = this.getRealPart() + a.getRealPart();
int i = this.getImaginPart() + a.getImaginPart();
return new Complex(r, i);
}
/**
* @return the realPart
*/
public int getRealPart() {
return realPart;
}
/**
* @param realPart
* the realPart to set
*/
public void setRealPart(int realPart) {
this.realPart = realPart;
}
/**
* @return the imaginPart
*/
public int getImaginPart() {
return imaginPart;
}
/**
* @param imaginPart
* the imaginPart to set
*/
public void setImaginPart(int imaginPart) {
this.imaginPart = imaginPart;
}
public static void main(String[] args) {
// 创建两个对象a,b
Complex a = new Complex(1, 2);
Complex b = new Complex(3, 4);
// 相加得到c
Complex c = a.complexAdd(b);
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
直接把上面复制,运行
给分吧
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询