自考Java语言程序设计(一)第六章重载与多态课后习题_第2页
{
m_dRealPart = r;
m_dImaginPart = i;
}
ComplexNumber(ComplexNumber c)
{
m_dRealPart = c.getRealPart();
m_dImaginPart = c.getImaginaryPart();
}
//get,set方法
double getRealPart()
{
return m_dRealPart;
}
double getImaginaryPart()
{
return m_dImaginPart;
}
void setRealPart(double d)
{
m_dRealPart = d;
}
void setImaginaryPart(double d)
{
m_dImaginPart = d;
}
//复数运算方法
ComplexNumber complexAdd(ComplexNumber c)
{
return new ComplexNumber(
this.m_dRealPart + c.getRealPart(),
this.m_dImaginPart + c.getImaginaryPart());
}
ComplexNumber complexAdd(double c)
{
return new ComplexNumber(
this.m_dRealPart + c, this.m_dImaginPart);
}
ComplexNumber complexMinus(ComplexNumber c)
{
return new ComplexNumber(
this.m_dRealPart - c.getRealPart(),
this.m_dImaginPart - c.getImaginaryPart());
}
ComplexNumber complexMinus(double c)
{
return new ComplexNumber(
this.m_dRealPart - c, this.m_dImaginPart);
}
ComplexNumber complexMulti(ComplexNumber c)
{
return new ComplexNumber(
this.m_dRealPart * c.getRealPart()
- this.m_dImaginPart * c.getImaginaryPart(),
this.m_dRealPart * c.getImaginaryPart()
+ this.m_dImaginPart * c.getRealPart());
}
ComplexNumber complexMulti(double c)
{
return new ComplexNumber(
this.m_dRealPart * c, this.m_dImaginPart * c);
}
//toString()
public String toString()
{
return "(" + m_dRealPart + " + "
+ m_dImaginPart + " i" + ")";
}
}
8.答:接口是用来实现类间多重继承功能的结构。接口与类相似,但是接口中只能包括常量和抽象方法。定义接口相当于定义了一组抽象的功能、属性集合,可以使了ava程序的类层次结构更加合理,并实现多重继承。
9.答:使用关键字implements可以定义类实现某接口。实现某接口的类如果不是抽象类,则需要通过重载来实现该接口中的所有抽象方法;如果这个类是抽象类,则它可以不必实现该接口中的所有抽象方法。
10.答:接口可以被继承。
SubInterface中的抽象方法有:public abstract int supMethod();和public abstract string subMethod();两个。
责编:admin