某软件系统中,已设计并实现了用于显示地址信息的类Address(如图6-1所示),现要求提供基于Dutch语言的地址信息显示接口。为了实现该要求并考虑到以后可能还会出现新的语言的接口,决定采用适配器(Adapter)模式实现该要求,得到如图6-1所示的类图。
图6-1适配器模式类图
【Java代码】
import java.util.*;
Class Address{
public void street( ){//实现代码省略}
public void zip( ){//实现代码省略}
public void city( ){//实现代码省略}
//其他成员省略
};
class DutchAddress{
public void straat( ){//实现代码省略}
public void postcode( ){//实现代码省略}
public void plaats( ){//实现代码省略}
//其他成员省略
};
class DutchAddressAdapter extends DutchAddress{
private(1);
public DutchAddressAdapter(Address addr){
address=addr;
}
public void straat( ){
(2);
}
public void postcode( ){
(3);
}
public void plaats( ){
(4);
}
//其他成员省略
};
class Test{
public static void main(String[]args){
Address addr=new Address( );
(5);
System.out.println("\n The DutchAddress\n");
testDutch(addrAdapter);
}
Static void?testDutch(DutchAddress addr){
addr.straat( );
addr.postcode( );
addr.plaats( );
}
}
正确答案及解析
正确答案
解析
(1)Address address;
(2)address.street();
(3)address.zip();
(4)address.city();
(5)DutchAddress addrAdapter=new DutchAddressAdapter(addr);
本题考查的是面向对象程序设计,结合设计模式。本题涉及的设计模式是适配器。
对于代码填空,可以参照类图和代码上下文补充。
首先理清类与类之间的继承关系,再根据上下文填写。
对于第(1)空,DutchAddressAdapter继承了DutchAddress方法,根据下面的同名构造函数可知,该类定义了一个名叫address的参数,而根据代码上下文可以,address的类型为Address。本空应该填写Address?address;
第(2)(3)(4)空是接口转换的具体实现,而在DutchAddressAdapter涉及的方法,可以从类图中找到,分别是straat(),postcode(),plaats(),适配器的目的是接口转换,即用这些方法分别展现原有Address中的street()、zip()、city()方法,因此这3个空分别填写address.street()、address.zip()、address.city()。
对于第(5)空,根据上下文最终调用testDutch方法的对象是addrAdapter,而此处是将原有的Address对象addr转换为接口对象,因此此处填写
DutchAddress addrAdapter=new?DutchAddressAdapter(addr)。
包含此试题的试卷
你可能感兴趣的试题
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
- 查看答案