如何转换 java.lang.Class到 java.lang.class;
请参阅底部的解决方案。
我正在尝试编写一些通用处理代码,但在其中一个子类中,它需要一个更具体的类。
因此,基类作为 Class 类型的字段,在子类中,我尝试将该 Class 对象转换为 Class 类型,它是 org.阿帕奇。哈杜普。数据库。 mapreduce.Mapper。
我从 Netbeans 收到以下错误:
"Incompatible types
required: java.lang.Class<org.apache.hadoop.hbase.mapreduce.TableMapper>
found: java.lang.class<capture#3 of ? extends org.apache.hadoop.mapreduce.Mapper>"
当我尝试以下代码时
Class<TableMapper> tableMapperClass = null;
if( mapperClass.equals(TableMapper.class) ) {
tableMapperClass = TableMapper.class.asSubclass(mapperClass);
//do stuff
}
,我得到:
incompatible types
required: java.lang.Class<org.apache.hadoop.hbase.mapreduce.TableMapper>
found: java.lang.Class<capture#8 of ? extends org.apache.hadoop.hbase.mapreduce.TableMapper>
好的
Class<TableMapper> tableMapperClass = null;
if( mapperClass.equals(TableMapper.class) ) {
tableMapperClass = mapperClass.asSubclass(TableMapper.class);
//do stuff
}
,从我的同事那里得到了答案,看起来应该可行:
Class<? extends TableMapper> tableMapperClass = null;
if( mapperClass.equals(TableMapper.class) ) {
tableMapperClass = mapperClass.asSubclass(TableMapper.class);
//do stuff
}
See the bottom for the solution.
I'm trying to write some generic handling code, but in 1 of the sub-classes, it requires a Class that is more specific.
So the base class as a field of type Class, and in the subclass I'm trying to cast that Class object to type Class which is a subclass of org.apache.hadoop.hbase.mapreduce.Mapper.
I get the following error from Netbeans:
"Incompatible types
required: java.lang.Class<org.apache.hadoop.hbase.mapreduce.TableMapper>
found: java.lang.class<capture#3 of ? extends org.apache.hadoop.mapreduce.Mapper>"
when I try the following code
Class<TableMapper> tableMapperClass = null;
if( mapperClass.equals(TableMapper.class) ) {
tableMapperClass = TableMapper.class.asSubclass(mapperClass);
//do stuff
}
and I get:
incompatible types
required: java.lang.Class<org.apache.hadoop.hbase.mapreduce.TableMapper>
found: java.lang.Class<capture#8 of ? extends org.apache.hadoop.hbase.mapreduce.TableMapper>
for
Class<TableMapper> tableMapperClass = null;
if( mapperClass.equals(TableMapper.class) ) {
tableMapperClass = mapperClass.asSubclass(TableMapper.class);
//do stuff
}
Ok, got the answer from my co-worker, looks like this should work:
Class<? extends TableMapper> tableMapperClass = null;
if( mapperClass.equals(TableMapper.class) ) {
tableMapperClass = mapperClass.asSubclass(TableMapper.class);
//do stuff
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想将一个类分配给实际上是 TableMapper 子类的 tableMapperClass,那么您需要更改变量的类型。相反,使用:
现在您可以将 TableMapper.class 或任何子类分配给此变量。当您编写
Class
时,您承诺该变量将恰好是 TableMapper.class 或 null。另一个例子:
请注意,您可以使用
Class
执行不同的操作,然后使用Class
。例如,您可以调用Class
的构造函数,因为您知道该类。你不能用Class< 来做到这一点吗? extends Number>
因为构造函数不是在编译时定义的。类似地,
List
与List
。您可以对List
类型的变量调用 list.add(7),但不能在List
因为您不知道第二个列表的类型。例如,它可能是一个List
,在这种情况下不允许添加 Integer。泛型很奇怪。 :) 希望这有帮助。
If you want to assign a class to tableMapperClass that is actually a subclass of TableMapper, then you need to change the type of the variable. Instead, use:
and now you can assign TableMapper.class or any subclass to this variable. When you write
Class<TableMapper>
you are promising that the variable will be exactly TableMapper.class or null.Another example:
Note that you can do different things with
Class<Number>
then you can withClass<? extends Number>
. For example, you can call a constructor ofClass<Number>
, because you know the class. You can't do that withClass<? extends Number>
because the constructors are not defined at compile time.Similarly with, say,
List<Number>
vsList<? extends Number>
. You can call list.add(7) on a variable of typeList<Number>
, but you can't do that on aList<? extends Number>
because you don't know the type of the second list. It might be aList<Double>
for example, in which case adding an Integer is not allowed.Generics are weird. :) Hope this helps.