readKeyFromConsoleAndInitPlanet 方法必须根据任务条件正确更新变量 thePlanet 的值。
如果输入的字符串是"sun",则将相应的对象分配给变量thePlanet.
package zh.codegym.task.task15.task1522;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
巩固单例模式
*/
public class Solution {
public static void main(String[] args) {
}
public static Planet thePlanet;
// 在此添加 static 块
static {
try {
readKeyFromConsoleAndInitPlanet();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void readKeyFromConsoleAndInitPlanet() throws IOException {
// 在此实现步骤 5
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String s = bufferedReader.readLine();
if(s.equals(Planet.EARTH)){
thePlanet=Earth.getInstance();
}
else if(s.equals(Planet.MOON)){
thePlanet=Moon.getInstance();
}
else if(s.equals(Planet.SUN)){
thePlanet=Sun.getInstance();
}
else thePlanet=null;
}
}