阅读下列说明和Java代码,将应填入(n)处的字句写在答题纸的对应栏内。 【说明】 某发票(lnvoice)由抬头(Head)部分、正文部分和脚注(Foot)部分构成。现采用装饰(Decorator)模式实现打印发票的功能,得到如图6-1所示的类图。【java代码】 class invoice{ public void printInvoice(){: System.out.println("This is the content of the invoice!"); } } class Decorator:extends Invoice{ protected Invoice ticket; public Decorator(lnvoice t){ ticket=t; } public void printinvoice(){ if(ticket!=NULL) (1); } } class FootDecorator extends Decorator{ public FootDecorator(lnvoice t){ super(t); } public void printinvoice(){ Systent.out.println("This is the header of the invoice!"); (2); } } class FootDecorator extends Decorator{ public FootDecorator(invoice t):{ super(t); } public void printlnvoice(){ (3); Systent.out.println("This is the header of the invoice!"); } } Class test{ public static void main(string[]args){ Invoice t=new invioce(); Invoice ticket; Ticket=(4); Ticket.Printinvoice(); Systent.out.println(“--------------“) Ticket=(5); Ticket.Printinvoice(); } } 程序的输出结果为: This is the header of the invoice! This is the content of the invoice! This is the footnote of the invoice! ---------------------------- This is the header of the invoice! This is the footnote of the invoice!

阅读下列说明和Java代码,将应填入(n)处的字句写在答题纸的对应栏内。 【说明】 某发票(lnvoice)由抬头(Head)部分、正文部分和脚注(Foot)部分构成。现采用装饰(Decorator)模式实现打印发票的功能,得到如图6-1所示的类图。

【java代码】 class invoice{ public void printInvoice(){: System.out.println("This is the content of the invoice!"); } } class Decorator:extends Invoice{ protected Invoice ticket; public Decorator(lnvoice t){ ticket=t; } public void printinvoice(){ if(ticket!=NULL) (1); } } class FootDecorator extends Decorator{ public FootDecorator(lnvoice t){ super(t); } public void printinvoice(){ Systent.out.println("This is the header of the invoice!"); (2); } } class FootDecorator extends Decorator{ public FootDecorator(invoice t):{ super(t); } public void printlnvoice(){ (3); Systent.out.println("This is the header of the invoice!"); } } Class test{ public static void main(string[]args){ Invoice t=new invioce(); Invoice ticket; Ticket=(4); Ticket.Printinvoice(); Systent.out.println(“--------------“) Ticket=(5); Ticket.Printinvoice(); } } 程序的输出结果为: This is the header of the invoice! This is the content of the invoice! This is the footnote of the invoice! ---------------------------- This is the header of the invoice! This is the footnote of the invoice!


参考解析

解析:(1) ticket.printInvoice() (2) ticket.printInvoice() (3) ticket.printInvoice() (4) new FootDecorator(new HeadDecorator(t)) (5) new FootDecorator(new HeadDecorator(new Decorator(null)))
【解析】

试题分析
本题考查的是面向对象程序设计和设计模式。本题涉及的设计模式是装饰模式。装饰模式(Decorator) :动态地给一个对象添加一些额外的职责。它提供了用子类打展功能的一个灵活的替代,比派生一个子类更加灵活。
对于程序填空可以参照代码上下文、题干说明和设计模式综合考虑。
对于第(1) 空,是对printInvoice方法的具体调用,在Decorator是 装饰类,继承了Invoice发票类。此处需要填写的是printInvoice方法的方法体,根据Decorator类的上下文,已定义ticket对象,所以此处调用printinvoice方法的是ticket,第(1) 空填写ticket printlnvoice()。
对于第(2) (3) 空,根据类图可知,分别是HeadDecorator抬头 类、FootDecorator脚注类调用printInvoice方法的方法体,由于在这两个类中并没有定义属性,只有借助其超类的构造函数,所以这两个地方调用printlnvoice方法的是它们的超类,即(2) (3) 填写的是super.printInvoice()。
对于第(4) (5) 空,考查的是对装饰模式的调用,都是main函数中实例化的过程,根据输出结果可以看到,第(4)空实例化ticket对象,可以输出抬头、内容、脚注3个部分, 因此需要调用三者的printInvoice()方法, 前面已经实例化了一个Invoice对象t,可以利用给子类实例化,因此第(4) 空填写new HeadDecorator(new FootDecorator());而第(5)空没有输出具体内容,只有抬头和脚注部分,可以看到这里的Invoice对象应该是空,所以第(5) 空填写new HeadDecorator(new FootDecorator(nl)。

相关考题:

试题八(共15分)阅读以下说明和Java程序代码,将应填入(n) 处的字句写在答题纸的对应栏内。[说明]在下面的 Java 程序代码中,类SalesTicket 能够完成打印票据正文的功能,类HeadDecorator 与FootDecorator 分别完成打印票据的台头和脚注的功能。已知该程序运行后的输出结果如下所示,请填补该程序代码中的空缺。这是票据的台头!这是票据正文!这是票据的脚注!------------------------这是票据的台头!这是票据的脚注![Java程序代码]public class SalesTicket {public void printTicket() {System.out.println("这是票据正文!");}}public class Decorator extends SalesTicket{SalesTicket ticket;}}public class FootDecorator extends Decorator{public FootDecorator(SalesTicket t) {(2) ;}public void printTicket() {super.printTicket();System.out.println("这是票据的脚注!");}}public class Main {public static void main(String[] args) {T = new HeadDecorator( (3) );T. (4) ;System.out.println("------------------------");T = new FootDecorator( (5) );T.printTicket();}}public Decorator(SalesTicket t){ticket = t;}public void printTicket(){if(ticket != null)ticket.printTicket();}}public class HeadDecorator extends Decorator{public HeadDecorator(SalesTicket t) {(1) ;}public void printTicket() {System.out.println("这是票据的台头!");super.printTicket();

阅读以下说明和流程图,将应填入(n)处的字句写在对应栏内。【说明】已知头指针分别为La和lb的有序单链表,其数据元素都是按值非递减排列。现要归并La和Lb得到单链表Lc,使得Lc中的元素按值非递减排列。程序流程图如下所示:

阅读下列说明和流程图,将应填入(n)的字句写在对应栏内。【说明】下列流程图(如图4所示)用泰勒(Taylor)展开式sinx=x-x3/3!+x5/5!-x7/7!+…+(-1)n×x2n+1/(2n+1)!+…【流程图】计算并打印sinx的近似值。其中用ε(>0)表示误差要求。

阅读以下说明和Jrdva代码,将应填入(n)处的字句写在对应栏内。[说明]在销售系统中常常需要打印销售票据,有时需要在一般的票据基础上打印脚注。这样就需要动态地添加一些额外的职责。如下展示了Decorator(修饰)模式。SalesOrder对象使用一个SalesTicket对象打印销售票据。图6-1显示了各个类间的关系。以下是Java语言实现,能够正确编译通过。[图6-1][Java代码]//Component.java文件public (1) class Component {abstract publ ic void prtTicket();}//salesTicket.java文件public class SalesTicket extends Component{public void prtTicket(){//Sales ticket printing code hereSystem.out.printin("SalesTicket");}}//Decorator.java文件publ ic abstract class Decorator extends Component{public void prtTicket(){if(myComp!=null)myComp.prtTicket();}private (2) myComp;public Decorator(Component myC){myComp=myC;}}//Footer.java文件public class Footer extends Decorator {public Footer(Component myC){(3);}public void prtTicket(){(4);prtFooter();}publ ic void prtFooter(){//place printing footer code hereSystem.out.println("Footer");}}//salesorder.java文件public class SalesOrder{void prtTicket(){Component myST;myST=new Footer( (5) );//Print Ticket with footers as neededmyST.prtTicket();}}(1)

试题五(共15分)阅读下列说明和C++-代码,将应填入 (n) 处的字句写在答题纸的对应栏内。【说明】某发票(lnvoice)由抬头(Head)部分、正文部分和脚注(Foot)部分构成。现采用装饰( Decorator)模式实现打印发票的功能,得到如图5-1所示的类图。【C++代码】include iostreamusing namespace std;class invoice{public:(1) {cout《 This is the content of the invoice!《 endl;}};class Decorator : public invoice {Invoice *ticket;public:Decorator(lnvoice *t) { ticket = t; }void printinvoice(){if(ticket != NULL)(2);}};class HeadDecorator : public Decorator{public:HeadDecorator(lnvoice*t): Decorator(t) { }void printinvoice0 {cout《 This is the header of the invoice! endl;(3) ;}};class FootDecorator : public Decorator{public:FootDecorator(invoice *t): Decorator(t) { }void printlnvoice() {(4) ;cout《 This is the footnote of the invoice!《 endl;}};int main(void) {Invoice t;FootDecorator f(t);HeadDecorator h(f);H.printlnvoice();cout “_____” endl;FootDecorator a(NULL);HeadDecorator b( (5) );B.printinvoice();return 0;}程序的输出结果为:This is the header of the invoice!This is the content of the invoice!This is the footnote of the invoice!----------------------------This is the header of the invoice!This is the footnote of the invoice!

试题六(共15分)阅读下列说明和Java代码,将应填入 (n) 处的字句写在答题纸的对应栏内。【说明】某发票(lnvoice)由抬头(Head)部分、正文部分和脚注(Foot)部分构成。现采用装饰( Decorator)模式实现打印发票的功能,得到如图6-1所示的类图。【java代码】class invoice{public void printInvoice(){:System.out.println ( This is the content of the invoice!);}}class Decorator : extends Invoice {protected Invoice ticket;public Decorator(lnvoice t){ticket = t;}public void printinvoice(){if(ticket != NULL)(1);}}class FootDecorator extends Decorator{public FootDecorator(lnvoice t){super(t);}public void printinvoice (){Systent.out.println( This is the header of the invoice! );(2) ;}}class FootDecorator extends Decorator {public FootDecorator(invoice t):{super(t);}public void printlnvoice(){(3) ;Systent.out.println( This is the header of the invoice! );}}Class test {public static void main(string[] args){Invoice t =new invioce();Invoice ticket;Ticket= (4) ;Ticket. Printinvoice();Systent.out.println(“--------------“)Ticket= (5) ;Ticket. Printinvoice();}}程序的输出结果为:This is the header of the invoice!This is the content of the invoice!This is the footnote of the invoice!----------------------------This is the header of the invoice!This is the footnote of the invoice!

阅读下列说明和c++代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】某灯具厂商欲生产一个灯具遥控器,该遥控器具有7个可编程的插槽,每个插槽都有开关按钮,对应着一个不同的灯。利用该遥控器能够统一控制房间中该厂商所有品牌灯具的开关,现采用Command(命令)模式实现该遥控器的软件部分。Command模式的类图如图5-1所示。【c++代码】}

阅读下列说明和Java代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】某灯具厂商欲生产一个灯具遥控器,该遥控器具有7个可编程的插槽,每个插槽都有开关按钮,对应着一个不同的灯。利用该遥控器能够统一控制房间中该厂商所有品牌灯具的开关,现采用Command(命令)模式实现该遥控器的软件部分。command模式的类图如图6-1所示。【Java代码】}

●试题一阅读下列说明和流程图,将应填入(n)的字句写在答题纸的对应栏内。【说明】下列流程图(如图4所示)用泰勒(Taylor)展开式sinx=x-x3/3!+x5/5!-x7/7!+…+(-1)n×x 2n+1/(2n+1)!+…【流程图】图4计算并打印sinx的近似值。其中用ε(0)表示误差要求。

试题三(共 15 分)阅读以下说明和 C 程序,将应填入 (n) 处的字句写在答题纸的对应栏内。

阅读下列函数说明和C++代码,将应填入(n)处的字句写在对应栏内。[说明]在销售系统中常常需要打印销售票据,有时需要在一般的票据基础上打印脚注。这样就需要动态地添加一些额外的职责。如下展示了Decorator(修饰)模式。SalesOrder对象使用一个SalesTicket对象打印销售票据,先打印销售票据内容,然后再打印脚注。图5-1显示了各个类间的关系。以下是C++语言实现,能够正确编译通过。[图5-1][C++代码]class Component{public:(1) void prtTicket()=0;};class SalesTicket:public Component{public:void prtTicket(){cout<<"Sales Ticket!"<<endl;}};class Decorator:public Component{public:virtual void prtTicket();Decorator(Component *myC);private:(2) myComp;};Decorator::Decorator(Component *myC){myComp=myC;}void Decorator::prtTicket(){myComp->prtTicket();}class Footer:public Decorator{public:Footer(Component *myC);void prtTicket();void prtFooter();};Footer::Footer(Component *myC): (3) {}void Footer::prtFooter(){cout<<"Footer"<<endl;}void Footer::prtTicket(){(4) ;prtFooter();}class SalesOrder{public:void prtTicket();};void SalesOrder::prtTicket(){Component *myST;myST=new Footer( (5) );myST->prtTicket();}(1)

图2-1是基于软交换的网络分层模型。请将选项应填入(n)处的字句写在答题纸对应的解答栏内。

(a)智能网概念模型中分布功能平面模型如下图所示,请根据此图将应填入(n)处的 字句写在答题纸的对应栏内。

图6-1是下一代网络的体系结构简图,请根据此简图将应填入(n)处的字句写在答题纸的对应栏内。

阅读以下说明和C++程序代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】在下面的C++代码中,类SalesTicket能够完成打印票据正文的功能,类HeadDec- orator与FootDecorator分别能够完成打印票据的台头和脚注的功能。已知该程序运行后的输出结果如下所示,请填补该程序代码中的空缺。这是票据的台头!这是票据正文!这是票据的脚注!---------------这是票据的台头!这是票据的脚注!【C++程序代码】#includeusing namespace std;class SalesTicket {public:(1) printTicket() { cout " 是票据正文!" endl;}class Decorator : public SalesTicket{SalesTicket *ticket;public:Decorator(SalesTicket *t){ ticket = t; }void printTicket(){if(ticket != NULL)ticket->printTicket();}};class HeadDecorator : public Decorator{public:HeadDecorator(SalesTicket *t): (2) { }void printTicket() {sour "这是票据的台头!" endl;Decorator::printTicket();}};class FootDecorator :public Decorator{public:FootDecorator(SalesTicket *t): (3)void printTicket() {Decorator::printTicket();cout "这是票据的脚注!" endl;}};void main(void) {SalesTicket t;FootDecorator f(t);HeadDecorator h( (4) );h.printTicket();cout "-------------------------" endl;FootDecorator a(NULL);HeadDecorator b( (5) );b.printTicket();}

阅读以下说明和Java程序代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】在下面的Java程序代码中,类SalesTicket能够完成打印票据正文的功能,类 HeadDecorator与FootDecorator分别能够完成打印票据的台头和脚注的功能。已知该程序运行后的输出结果如下所示,请填补该程序代码中的空缺。这是票据的台头!这是票据正文!-------------------这是票据的脚注!这是票据的台头!这是票据的脚注!【tava程序代码】public class SalesTicket {public void printTicket() {System. out. println ( "这是票据正文 ! ");}}public class Decorator extends SalesTicket{SalesTicket ticket;public Decorator(SalesTicket t) {ticket = t;}public void printTicket(){if(ticket != null)ticket.printTicket();}public class HeadDecorator extends Decorator{public HeadDecorator(SalesTicket t) {(1)}public void printTicket() {System. out.println("这是票据的台头! ");super.printTicket();}}public class FootDecorator extends Decorator{public FootDecorator(SalesTicket t) {(2);}public void printTicket() {super, printTicket ();System. out.println ("这是票据的脚注!");}}public class Main {public static void main(String[] args) {T = new HeadDecorator( (3));T=(4);System.out.println("------------------------------------");T = new FootDecorator((5));T.printTicket ();}}

阅读下列说明和Java代码,将应填入(n)处的字句写在对应栏内。某饭店在不同的时段提供多种不同的餐饮,其菜单的结构图如图5-1所示。现在采用组合(Composition)模式来构造该饭店的菜单,使得饭店可以方便地在其中增加新的餐饮形式,得到如图5-2所示的类图。其中MenuComponent为抽象类,定义了添加(add)新菜单和打印饭店所有菜单信息(print)的方法接口。类Menu表示饭店提供的每种餐饮形式的菜单,如煎饼屋菜单、咖啡屋菜单等。每种菜单中都可以添加子菜单,例如图5-1中的甜点菜单。类MenuItem表示菜单中的菜式。

阅读下列说明和 C ++代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】某软件公司欲开发一款汽车竞速类游戏,需要模拟长轮胎和短轮胎急刹车时在路面上留 下的不同痕迹,并考虑后续能模拟更多种轮胎急刹车时的痕迹。现采用策略(Strategy)设计模式来实现该需求,所设计的类图如图 6-1 所示。【C++ 代码】#includeusing namespace std;class BrakeBehavior{public:(1) ; /*其余代码省略*/};class LongWheelBrake : public BrakeBehavior{public:void stop(){cout

阅读下列说明和C++-代码,将应填入(n)处的字句写在答题纸的对应栏内。 【说明】 某发票(lnvoice)由抬头(Head)部分、正文部分和脚注(Foot)部分构成。现采用装饰(Decorator)模式实现打印发票的功能,得到如图5-1所示的类图。【C++代码】 #include using namespace std; class invoice{ public: (1){ cout

阅读下列说明和Java代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】某软件公司欲开发一款汽车竞速类游戏,需要模拟长轮胎和短轮胎急刹车时在路面上留 下的不同痕迹,并考虑后续能模拟更多种轮胎急刹车时的痕迹。现采用策略(Strategy)设计模式来实现该需求,所设计的类图如图 5-1 所示。【Java代码】import java.util.*; interface BrakeBehavior{public (1) ;/*其余代码省略*/}class LongWheelBrake implements BrakeBehavior{public void stop(){System.out.println("模拟长轮胎刹车痕迹! ");}/*其余代码省略*/}class ShortWheelBrake implements BrakeBehavior {public void stop(){System.out.println("模拟短轮胎刹车痕迹! ");}/*其余代码省略 */}abstract class Car{protected (2) wheel;public void brake(){ (3) ;}/*其余代码省略*/}class ShortWheelCar extends Car {public ShortWheelCar(BrakeBehavior behavior){(4) ; } /*其余代码省略*/}class StrategyTest{public static void main(String[]args){BrakeBehaviorbrake =new ShortWheelBrake();ShortWheelCar car1= new ShortWheelCar(brake);car1. (5) ;}}

阅读下列说明,回答问题1和问题2,将解答写在答题纸的对应栏内。【说明】 Windows 系统的用户管理配置中,有多项安全设置,如图2-1 所示。

阅读下列说明和C++代码,回答问题,将解答填入答题纸的对应栏内。【说明】某航空公司的会员积分系统将其会员划分为:普卡 (Basic)、银卡(Silver)和金卡 (Gold) 三个等级。非会员 (NonMember) 可以申请成为普卡会员。会员的等级根据其一年内累积 的里程数进行调整。描述会员等级调整的状态图如图 5-1 所示。现采用状态 (State) 模式实现上述场景,得到如图 5-2 所示的类图。【问题1】(15分)阅读上述说明和C++代码,将应填入 (n) 处的字句写在答题纸的对应栏内。

阅读下列说明和 Java 代码,将应填入(n)处的字句写在答题纸的对应栏内。 【说明】 某软件公司欲开发一款汽车竞速类游戏,需要模拟长轮胎和短轮胎急刹车时在路面上 留 下的不同痕迹,并考虑后续能模拟更多种轮胎急刹车时的痕迹。现采用策略(Strategy) 设 计模式来实现该需求,所设计的类图如图 5-1 所示。

阅读下列说明和C++代码,回答问题,将解答填入答题纸的对应栏内。【说明】某灯具厂商欲生产一个灯具遥控器,该遥控器具有7个可编程的插槽,每个插槽都有开关按钮,对应着一个不同的灯。利用该遥控器能够统一控制房间中该厂商所有品牌灯具的开关,现采用Command(命令)模式实现该遥控器的软件部分。Command模式的类图如下图所示。Command?模式类图

阅读下列说明和C++-代码,将应填入 (n) 处的字句写在答题纸的对应栏内。【说明】 某发票(lnvoice)由抬头(Head)部分、正文部分和脚注(Foot)部分构成。现采用装饰( Decorator)模式实现打印发票的功能,得到如图5-1所示的类图。【C++代码】#includeusingnamespace std;class Invoice{public: (1) { coutinvoice!" }};classDecorator : public Invoice { Invoice *ticket;public: Decorator(lnvoice *t) { ticket = t; } void printInvoice( ){ if(ticket != NULL) (2); } };classHeadDecorator : public Decorator{public: HeadDecorator(lnvoice*t): Decorator(t) { } void printInvoice( ) { coutheader of the invoice! " (3) ; } }; class FootDecorator : public Decorator{ public: FootDecorator(Invoice *t): Decorator(t) { } void printlnvoice( ){ (4) ; coutfootnote of the invoice!" }};int main(void){ Invoice t; FootDecorator f( HeadDecorator h( h.printInvoice( ); cout FootDecorator a(NULL) ; HeadDecorator b( (5) ); b.printInvoice( ); return 0;}程序的输出结果为: This is the header of the invoice! This is the content of the invoice! This is the footnote of the invoice! ---------------------------- This is the header of the invoice! This is the footnote of the invoice!

阅读下列说明和 Java代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】某航空公司的会员积分系统将其会员划分为:普卡 (Basic) 、银卡(Silver)和金卡 (Gold)三个等级。非会员 (NonMember)可以申请成为普卡会员。会员的等级根据其 一年内累积的里程数进行调整。描述会员等级调整的状态图如图 6-1 所示 。现采用状态 (State) 模式实现上述场景,得到如图 6-2 所示的类图。

阅读下列说明和Java代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】 某文件管理系统中定义了类OfficeDoc和DocExplorer,当类OfficeDoc发生变化时,类DocExplorer的所有对象都要更新其自身的状态,现采用观察者(Observer)设计模式来实现该需求,所设计的类图如图6-1所示。

阅读下列说明和?C++代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】阅读下列说明和?Java代码,将应填入?(n)?处的字句写在答题纸的对应栏内。【说明】某快餐厅主要制作并出售儿童套餐,一般包括主餐(各类比萨)、饮料和玩具,其餐品种类可能不同,但其制作过程相同。前台服务员?(Waiter)?调度厨师制作套餐。现采用生成器?(Builder)?模式实现制作过程,得到如图?6-1?所示的类图。