如何以C#类格式编写此Java类

时间:2019-06-19 11:21:40

标签: java c# static

如何以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");
    }
   }
}

0 个答案:

没有答案