我有这个类 [1],我尝试将其与我的类 [2] 一起使用。但是,MyIdentityMapper
的泛型是错误的。我想创建一个通用类,当我使用它时(MyIdentityMapper
),我会传递正确的类型。我应该如何在 MyIdentityMapper
中使用泛型?
[1]映射器类:
public class Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT> {...}
[2]身份类别:
public class MyIdentityMapper extends Mapper<K,V,K,V> {
public void map(K key, V value, Context context) throws IOException, InterruptedException {
context.write(key, value);
}
}
最佳答案
您需要在 MyIdentityMapper
上声明 K
和 V
类型参数,以便可以在类的其余部分中引用它们。
public class MyIdentityMapper<K, V> // ... rest doesn't change
关于java - 泛型的使用是子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33656479/