难道我不是这么做的吗?
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 {
readKeyFromConsoleAndInitPlanet();
}
public static void readKeyFromConsoleAndInitPlanet() {
// 在此实现步骤 5
try {
BufferedReader reader =new BufferedReader(new InputStreamReader(System.in));
String s=reader.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;
}catch (IOException e){
System.out.println("输入输出异常");
}
}
}