问答题

【说明】
某绘图系统存在point、line、square三种图元,它们具有Shape接口,图元的类图关系如图13-12所示。现要将circle图元加入此绘图系统以实现功能扩充。已知某第三方库已经提供了XCircle类,且完全满足系统新增的Circle图元所需的功能,但XCircle不是由 Shape派生而来的,它提供的接口不能被系统直接使用。代码13-2既使用了XCircle又遵循了Shape规定的接口,既避免了从头开发一个新的Circle类,又可以不修改绘图系统中已经定义的接口。代码13-3根据用户指定的参数生成特定的图元实例,并对之进行显示操作。绘图系统定义的接口与XCircle提供的显示接口及其功能如表13-5所示。
表13-5接口及其功能
Shape
XCircle
功能
display()
DisplayIt()
显示图元


【代码13-2】
class Circle (1) {
private (2) pxc;
public Circle(){
pxc=new (3)
}
public void display(){
pxc. (4)
}
}
【代码13-3】
public class Factory{
public (5) getShape Instance(int tyoe){ //生成特定类实例
switch(type){
case 0: return new point();
case 1: return new Rectangle();
case 2: return new line();
case 3: return new Circle();
default: return null
}
}
};
public class App{
public static viod main(String argv[
){
if(argv. length!=1){
system. out. println("error parameters!");
Return;
}
int type=(new Integer(argv[0
)). intValue();
Factory factory=new Factory();
shape s;
s=factory. (6) ;
if(s==null){
system.out. println("Error get instance!");
Return;
}
s.display();
return;
}
}

【参考答案】

(1)implements Shape (2)XCircle (3)XCircle() (4......

(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)
热门 试题

问答题
【说明】在一公文处理系统中,开发者定义了一个公文结构OfficeDoc,其中定义了公文应该具有的属性。当公文的内容或状态发生变化时,与之相关联的DocExplorer结构的值都需要发生改变。一个OfficeDoc结构能够关联一组DocExplorer结构。当OfficeDoc结构的内容或状态发生变化时,所有与之相关联的DocExplorer结构都将被更新,这种应用被称为观察者模式。以下代码采用C语言实现,能够正确编译通过。【代码13-4】# include<stdio.h># define OBS_MAXNUM 20 *一个OfficeDoc变量最多能够关联的DocExplorer变量的个数* typedef void( (1) )(struc OffieeDoc*, struct DoeExplorer*)I;struct DocExplorer{func update; *DocExplorer结构采用的更新函数* *其它的结构字段省略* };struet OffieeDoc{(2) myObs[OBS_MAXNUM; *存储所有与OfficeDoc相关联的DocExplorer结构指针* int index; *与OffieeDoc结构变量相关联的DoeExplorer结构变量的个数* };void attaeh(struct OfficeDoc*doc, struct DocExplorer*ob){ *关联Observer结构ob与OffieeDoe结构doe* int loop=0;if(doc->index>=OBS_MAXNUM||ob==NULL)return;for(loop=0, loop<doc->index; loop++)if(doc->myObs[loop]==ob)return;doc->myObs[doe->index]=ob;doc->index++;}void detaeh(struct OfficeDoc*doc, struct DocExplorer*ob){ *解除doc结构与ob结构间的关联* int loop;if(ob==NULL)return;for(loop=0;loop<doc->index; loop++){if(doe->myObs[loop]==ob){if(loop<=doc->index-2)doc->myObs[loop]=doc->myObs[ (3) ];doc->myObs[doc->index-1]=NULL;doc->index——;breack;}}}void updatel(struct OfficeDoe*doe, struct DoeExplorer *ob){ *更新ob结构的值,更新代码省略* } void update2(struct OffieeDoc*doc,struet DocExplorer *ob){ *更新ob结构的值,更新代码省略* }void notifyObs(struct OfficeDoc* doc){ *当doc结构的值发生变化时,通知与之关联的所有DocExplorer结构变量* int loop;for(loop=0; loop<doc->index; loop++){(doc->myObs[loop)->update( (4) );}}void main(){struct OfficeDoc doc; *定义一了OfficeDoe变量* struct DocExplorer explorer1, explorer2; *定义两个DocExplorer变量* *初始化与OfficeDoc变量相关的DocExplorer变量个数为0* doc.index=0;explorer1.update=update1; *设置explorer1变量的更新函数* explorer2. update=update2; *设置explorer2变量的更新函数* attach(&doc, &explorer1); *关联explorer1与doc对象* attach(&doc, &explorer2); *关联explorer2与doc对象* *其它代码省略* (5) ; *通知与OfficeDoe相关的所有DoeExploer变量* return;}
问答题
【说明】软件设计师东方飞龙利用UML设计了一个迷你小型复数类,其类图如图13-11所示。 【代码13-l】 *___________________________________* ********* 文件 MiniComplex. h********* *___________________________________* #include<iostream>using namespace std;class MiniComplex{ (1) : 重载流插入和提取运算符(2) ostream & operator <<(ostream & osObject, const MiniComplex & complex){ osObject << ( <<complex. realPart<< + <<complex. imagPart << I << ) ;return osObject;}friend (3) operator >>(istream & isObject, MiniComplex & complex){ char ch;isObject >>complex. realPart >>ch>>complex. imagPart >>ch;return isObject;}MiniComplex(double real=0, double imag=0); 构造函数MiniComplex operator+(const MiniComplex & otherComplex)const! 重载运算符+MiniComplex operator--(const MiniComplex & otherComplex)const! 重载运算符-MiniComplex operator*(const MiniComplex& othmComplex)const; 重载运算符*MiniComplex operator (const MiniComplex & otherComplex)const; 重载运算符 bool operator==(const MiniComplex &otherComplex)const; 重载运算符==private:double realPart; 存储实部变量double imagPart; 存储虚部变量}; *_______________________________________________________* * * * * * * * * *文件 MiniComplex. cpp* * * * * * * * * * *_______________________________________________________* # include MiniComplex.h bool MiniComplex:: operator==(const MiniComplex & otherComplex)const{ (1) ;}MiniComplex:: MiniComplex(double real, double imag){realPart=real;imagPart=imag!}MiniComplex MiniComplex:: operator+(const MiniComplex & otherComplex)const{ MiniComplex temp;temp. realPart=realPart+ otherComplex. realPart;temp. imagPart=imagPart+ otherComplex. imagPart;return temp;}MiniComplex MiniComplex::operator--(const MiniComplex & otherComplex)const{ MiniComplex temp;temp.realPart=realPart-otherComplex.realPart;temp. imagPart=imagPart-otherCompler.imagPart;return temp;}MiniComplex MiniComplex:: operator*(const MiniComplex& otherComplex)const{ MiniComplex temp;temp.realPart=(realPart* otherComplex.realPart)-(imag-Part* otherComplex.imag-Part);temp imagPart=(realPart* otherComplex. imagPart)+(imag-Part *otherComplex.realPart);return temp,}MiniComplex MiniComplex:: operator (const MiniComplex& otherComplex)eonst{ MiniComplex temp;float tt;tt=1 (otherComplex. realPart *otherComplex. realPart+otherComplex. imagPart* other Complex.imagPart);temp. realPart=((realPart* otherComplex.realPart)+(imagPart* otherComplex.imagPart))*tt;temp. imagPart=((imagPart * otherComplex.realPart)-(realPart* otherComplex.imagPart))*tt;return temp;} *__________________________________________________* * * * * * * * *主函数所在文件main.cpp* * * * * * * * *_________________________________________________* # include<iostream># include (5) using namespace std;int main(void){ MiniComplex num1(23, 34), num2;cin>>num2;cout<< Initial Value of Numl= <<num1<< nInitial Value of Num2= <<num2<<end1;cout<<num1<< + <<num2<< = <<num1+num2<<end1; 使用重载的加号运算符cout<<num1<< - <<num2<< = <<num1-num2<<end1; 使用重载的减号运算符cout<<num1<< * <<num2<< - <<num1*num2<<end1; 使用重载的乘号运算符cout<<num1<< <<num2<< = <<num1 num2<<end1; 使用重载的除号运算符return 0;}