首页
登录
从业资格
阅读下列说明和Java代码,将应填入(n)处的字句写在答题纸的对应栏内。
阅读下列说明和Java代码,将应填入(n)处的字句写在答题纸的对应栏内。
考试题库
2022-08-02
96
问题
阅读下列说明和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)。
转载请注明原文地址:https://tihaiku.com/congyezige/2424909.html
本试题收录于:
初级程序员题库软件水平考试初中高级分类
初级程序员
软件水平考试初中高级
相关试题推荐
IE浏览器能够正确解析()代码。A.ASP B.HTML C.JSP D
网页中代码"inputtype=textname="foo"size=20
负责解释执行JavaScript代码的是()。A.Web服务器 B.Web浏览
下面的XML代码段中,语法正确的是()。A.<!-xml示例-!><?xml
()不是蠕虫病毒。A.冰河 B.红色代码 C.熊猫烧香 D.爱虫病毒
()属于系统软件,它直接执行高级语言源程序或与源程序等价的某种中间代码。A.编译
以下HTML代码中,创建指向邮箱地址的链接正确的是()A.<ahref="
在html文档中,有如下代码: <form> List1:
HTML页面的“<title>主页</title>”代码应写在()标记内A.
要在HTML代码中加入注释,应使用()来标记。A.<!----> B./
随机试题
[originaltext](5)Egyptianofficialsaremakingplanstoopenanewly-expan
元代(theYuanDynasty)是中国瓷器(porcelain)发展史上一个重要的时期。基于先前朝代的烧制技术,景德镇成功地制作出青花瓷(the
唯有选好典型,()才能起到应有的作周,如选材不典型,就不能发挥它应有的作用。A
女性,27岁,初产妇,产后3周前感右侧乳房疼痛,当时未做特殊处理,近3天出现寒战
尿路感染女性发病率高于男性,是因为女性尿道较男性尿道A.短而宽 B.长而窄
风痰阻络所致的癫痫静息期除选主穴外还可配用A.心俞、脾俞、足三里 B.曲池、神
蜂蜜是一种高糖溶液,主要成分是葡萄糖,果糖,占蜂蜜的65%-80%,糖的含量远高
李娜将刘鑫诉至法院,要求刘鑫依照借款合同的约定清偿债务,在诉讼进行过程中,李娜将
(2020年真题)关于药物制成剂型意义的说法,错误的是A.可改变药物的作用性质
标准施工合同签订合同时签约合同价内尚不确定的款项包括()。A.暂估价 B.
最新回复
(
0
)