如何设置类的名称?

时间:2013-07-23 15:58:30

标签: java generics

我的代码是:

public static <K, V> void initMapperJob( Job job,
        Collection<WebPage.Field> fields,  Class<K> outKeyClass, Class<V>
        outValueClass,   Class<? extends GoraMapper<String, WebPage, K, V>> mapperClass,     
        Class<? extends Partitioner<K, V>> partitionerClass, boolean reuseObjects)   
        throws ClassNotFoundException, IOException {
               DataStore<String, WebPage> store = createWebStore(job.getConfiguration(), String.class,    WebPage.class );
            ........ 
    }

我改变如下:

        public static <K, V, P extends PersistentBase, F extends Fields> void initMapperJob(Job job,
          Collection<F> fields,
          Class<K> outKeyClass, Class<V> outValueClass,
          Class<? extends GoraMapper<String, P, K, V>> mapperClass,
          Class<? extends Partitioner<K, V>> partitionerClass, boolean reuseObjects)
      throws ClassNotFoundException, IOException {

        DataStore<String, P> store = createWebStore(job.getConfiguration(),
            String.class, P.class );

....
      }

我的问题是:如何通过java为以下行设置泛型类?

  

DataStore store = createWebStore(job.getConfiguration(),                   String.class, P.class );

如果我设置为(P.class或P),则发生错误。

调用以下方法。

public static <K, V extends Persistent> DataStore<K, V> createWebStore(Configuration conf,
      Class<K> keyClass, Class<V> persistentClass){...}

1 个答案:

答案 0 :(得分:1)

您需要添加Class<P>类型的参数并使用它(而不是P.class)。然后,您需要转到initMapperJob()所在的站点,并将该额外参数添加到呼叫中。

    public static <K, V, P extends PersistentBase, F extends Fields> 
    void initMapperJob(Job job,
      Collection<F> fields,
      Class<K> outKeyClass, Class<V> outValueClass,
      Class<? extends GoraMapper<String, P, K, V>> mapperClass,
      Class<? extends Partitioner<K, V>> partitionerClass, 
      boolean reuseObjects,
      Class<P> pClass) throws ClassNotFoundException, IOException {

    DataStore<String, P> store = createWebStore(job.getConfiguration(),
        String.class, pClass);

    ....
  }