2009年2月9日 星期一

第65題

Replace two of the Modifiers that appear in the Single class to make the code compile.
Note: Three modifiers will not be used and four modifiers in the code will remain unchanged.

Code
public class Single {
private static Single instance;
public static Single getInstance() {
if (instance == null) instance = create();
return instance;
}
private Single() { }
protected Single create() { return new Single(); }
}
class SingleSub extends Single {
}
Modifiers
final
protected
private
abstract
static

答案:

1.class Single{
2.private static Single instance;
3.public static Single getInstance(){
4.if(instance == null) instance = create();
5.return instance;
6.}
7.
8.protected Single(){}
static Single create(){return new Single();}
9.}
10.class SingleSub extends Single{
11.}

參考:7-3 存取修飾詞的使用、8-2 方法種類與呼叫方式

沒有留言:

張貼留言