某咖啡店当卖咖啡时,可以根据顾客的要求在其中加入各种配料,咖啡店会根据所加入的配料来计算费用。咖啡店所供应的咖啡及配料的种类和价格如下表所示。
现采用装饰器(Decorator)模式来实现计算费用的功能,得到如图6-1所示的类图
【Java代码】
import java.util.*;
(1)class Beverage{//饮料
String description="Unknown Beverage";
public(2)( ){return description;}
public(3);
}
abstract class CondimentDecorator extends Beverage{//配料
(4);
}
class Espresso extends Beverage{//蒸馏咖啡
private final int ESPRESSO_PRICE=25;
public Espresso( ){description="Espresso";}
public int cost( ){return ESPRESSO_PRICE;}
}
class DarkRoast extends Beverage{//深度烘焙咖啡
private finalint DARKROAST_PRICE=20;
public DarkRoast0{description="DarkRoast";}
public int cost( ){rcturn DARKROAST PRICE;}
}
class Mocha extends CondimentDecorator{//摩卡
private final int MOCHA_PRICE=10;
public Mocha(Beverage beverage){
this.beverage=beverage;
}
public String getDescription( ){
return beverage.getDescription0+",Mocha";
}
public int cost( ){
return MOCHA_PRICE+beverage.cost( );
}
}
class Whip extends CondimentDecorator{//奶泡
private finalint WHIP_PRICE=8;
public Whip(Beverage beverage){this.beverage=beverage;}
public String getDescription( ){
return beverage.getDescription( )+",Whip";
}
public int cost( ){return WHIP_PRICE+beverage.cost( );}
}
public class Coffee{
public static void main(String args[]){
Beverage beverage=new DarkRoast( );
beverage=new Mocha((5));
beverage=new Whip((6));
System.out.println(beverage.getDescription0+"¥"+beverage.cost( ));
}
}
编译运行上述程序,其输出结果为:
DarkRoast,Mocha,Whip¥38
正确答案及解析
正确答案
解析
(1)abstract
(2)String getDescription
(3)abstract int cost()
(4)Beverage beverage
(5)beverage
(6)beverage
本题考查了Java语言的应用能力和装饰设计模式的应用。
第(1)空很明显,是要给类Beverage前添加定义的关键字,从整个程序来看,我们应该要将类Beverage定义为抽象类,需要在前面添加关键字abstract,因此第(1)空的答案为abstract。
第(2)空处也很明显,是要给出一个函数的定义,并且该函数的函数体是“return description;”,从子类奶泡和摩卡中我们不难发现这个函数应该是getDescription,而该函数的返回类型String,因此本空的答案为String getDescription。
第(3)空需要结合后面各子类才能发现,在Beverage中还应该定义一个函数cost(),而这个函数在Beverage中并没有实现,因此要定义为抽象函数,所以第(3)空的答案为abstract int cost()=0。
第(4)空在类CondimentDecorator中,且是该类唯一的一条语句,而他的子类分别是奶泡和摩卡,在奶泡和摩卡这两个类中,都用到了Beverage beverage,而在使用之前并没有说明,因此这就可以判定,Beverage beverage是在父类CondimentDecorator中定义的,子类直接继承使用,因此第(4)空的答案为Beverage beverage。
第(5)和第(6)空在主函数当中,其中第(5)空是要创建一个Mocha对象,应该调用的是类Mocha的构造函数,从类Mocha中,我们可以看出,其构造函数Mocha的参数是一个Beverage类型的对象引用,而在主函数中,开始就定义了一个Beverage类型的对象引用beverage,因此这里只需填写beverage即可。同理第(6)空的答案也是beverage。
包含此试题的试卷
你可能感兴趣的试题
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
- 查看答案