如何以C#类格式编写此Java类
public final class JsonParserResolver {
// PlacesApiJsonParser is an Interface
public static final PlacesApiJsonParser JSON_PARSER;
static {
boolean hasGson;
try {
Class.forName("com.google.gson.Gson");
hasGson = true;
} catch (ClassNotFoundException e) {
hasGson = false;
}
JSON_PARSER = hasGson ? new GsonPlacesApiJsonParser() : new
AndroidPlacesApiJsonParser();
}
private JsonParserResolver() {
throw new RuntimeException("No instances");
}
}
类静态成员在类实例化时自动解决问题。这是非常有用的
抱住我的马,我在这里JSON_PARSER
namespace WorkSampleBookSearch
{
public final class JsonParserResolver
{
public static final PlacesApiJsonParser JSON_PARSER;
static {
boolean hasGson;
JSON_PARSER = hasGson? new GsonPlacesApiJsonParser() : new AndroidPlacesApiJsonParser();
}
private JsonParserResolver()
{
throw new RuntimeException("No instances");
}
}
}