某发票(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 HeadDecorator extends Decorator{
public HeadDecorator(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 footnote 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)super.printInvoice()
(3)super.printInvoice()
(4)new HeadDecorator(new FootDecorator(t))
(5)new HeadDecorator(new FootDecorator(null))
本题考查的是面向对象程序设计和设计模式。本题涉及的设计模式是装饰模式。
装饰模式(Decorator):动态地给一个对象添加一些额外的职责。它提供了用子类扩展功能的一个灵活的替代,比派生一个子类更加灵活。
对于程序填空可以参照代码上下文、题干说明和设计模式综合考虑。
对于第(1)空,是对printInvoice方法的具体调用,在Decorator是装饰类,继承了Invoice发票类。此处需要填写的是printInvoice方法的方法体,根据Decorator类的上下文,已定义ticket对象,所以此处调用printInvoice方法的是ticket,第(1)空填写ticket.printInvoice()。
对于第(2)(3)空,根据类图可知,分别是HeadDecorator抬头类、FootDecorator脚注类调用printInvoice方法的方法体,由于在这两个类中并没有定义属性,只有借助其超类的构造函数,所以这两个地方调用printInvoice方法的是它们的超类,即(2)(3)填写的是super.printInvoice()。
对于第(4)(5)空,考查的是对装饰模式的调用,都是main函数中实例化的过程,根据输出结果可以看到,第(4)空实例化ticket对象,可以输出抬头、内容、脚注3个部分,因此需要调用三者的printInvoice()方法,前面已经实例化了一个Invoice对象t,可以利用t给子类实例化,因此第(4)空填写new HeadDecorator(new FootDecorator(t));而第(5)空没有输出具体内容,只有抬头和脚注部分,可以看到这里的Invoice对象应该是空,所以第(5)空填写new HeadDecorator(new FootDecorator(null))。
包含此试题的试卷
你可能感兴趣的试题
Advancements in ( )have contributed to the growth of the automotive industry through the creation and evolution of self-driving vehicles.
-
- A.Artificial Intelligence
- B.Cloud Computing
- C.Internet of Things
- D.Big Data
- 查看答案
In project human resource management , ( )is not a source of power for the project manager.
-
- A.referent power
- B.expert power
- C.reward power
- D.audit power
- 查看答案
At the project establishment stage , the feasibility study mainly includes techinical feasibility analysis , ( ), operation environment feasibility analysis and other aspects of feasibility analysis.
-
- A.detail feasibility analysis
- B.opportunity analysis
- C.economic feasibility analysis
- D.risk analysis
- 查看答案
( )is a grid that shows the project resources assigned to each work package.
-
- A.Stakeholder engagement assessment matrix
- B.Requirements traceability matrix
- C.Probability and impact matrix
- D.Responsibility assignment matrix
- 查看答案
Xinhua News Agency reported in January 2022,Chian will further promote the developmet of a digital economy during the 14th Five-Year Plan eriod(2021-2025). The plan also emphasized industrial ( )transformation.
-
- A.digital
- B.networking
- C.intelligentize
- D.informatization
- 查看答案