阅读以下说明和Java代码,填补代码中的空缺,将解答填入答题纸的对应栏内。
【说明】
以下Java代码实现一个超市简单销售系统中的部分功能,顾客选择图书等物件 (Item)加入购物车(ShoppingCart),到收银台(Cashier)对每个购物车中的物品统计其价格进行结账。设计如图5-1所示类图。
【Java代码】
interface Item{ public void accept(Visitor visitor); public double getPrice();}class Book (1){ private double price; public Book(double price){(2);} public void accept(Visitor visitor){ //访问本元素 (3); } public double getPrice() { return price; }}//其它物品类略 interface Visitor { public void visit(Book book); //其它物品的visit方法 } class Cashier(4){ private double totalForCart; //访问Book类型对象的价格并累加 (5){ //假设Book类型的物品价格超过10元打8折 if(book.getPrice()<10.0){ totalForCart+=book.getPrice(); } else totalForCart+=book.getPrice()*0.8; } //其它visit方法和折扣策略类似,此处略 public double getTotal() { return totalForCart; }} class ShoppingCart { //normal shopping cart stuff private java.util.ArrayListitems=newjava.util.ArrayList<>(); public double calculatePrice() { Cashier visitor=newCashier(); for(Item item:items) { (6); } doubletotal=visitor.getTotal(); return total; } public void add(Item e) { this.items.add(e); }}
正确答案及解析
正确答案
解析
(1)implements Item
(2)this.price=price
(3)visitor.visit(this)
(4)implements Visitor
(5)public void visit(Book book)
(6)item.accept(visitor)【解析】
这里考察的是访问者模式。其定义如下:封装某些作用于某种数据结构中各元素的操作,它可以在不改变数据结构的前提下定义作用于这些元素的新的操作
第一、四空为接口与实现,接口使用Interface,实现使用implements。第二空this表示类实例本身。第三空为访问本元素。第五空实现接口里面的方法。第六空调用accept方法
包含此试题的试卷
你可能感兴趣的试题
( )is the process of transforming information so it is unintelligible to anyone but the intended recipient.
-
- A.Encryption
- B.Decryption
- C.Security
- D.Protection
- 查看答案
As each application module is completed,it undergoes( )to ensure that it operates correctly and reliably.
-
- A.unit testing
- B.integration testing
- C.system testing
- D.acceptance testing
- 查看答案
( )algorithm specifies the way to arrange data in a particular order.
-
- A.Search
- B.Random
- C.Sorting
- D.Merge
- 查看答案
After analyzing the source code,( )generates machine instructions that will carry out the meaning of the program at a later time.
-
- A.an interpreter
- B.a linker
- C.a compiler
- D.a converter
- 查看答案
( )can help organizations to better understand the information contained within the data and will also help identify the data that is most important to the business and future business decisions.
-
- A.Data processing system
- B.Big Data analytics
- C.Cloud computing
- D.Database management
- 查看答案