如何转换 java.lang.Class到 java.lang.class;

发布于 2024-12-06 11:30:26 字数 1329 浏览 1 评论 0原文

请参阅底部的解决方案。

我正在尝试编写一些通用处理代码,但在其中一个子类中,它需要一个更具体的类。

因此,基类作为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻梦旅人 2024-12-13 11:30:26

如果您想将一个类分配给实际上是 TableMapper 子类的 tableMapperClass,那么您需要更改变量的类型。相反,使用:

Class<? extends TableMapper> tableMapperClass = null;

现在您可以将 TableMapper.class 或任何子类分配给此变量。当您编写 Class 时,您承诺该变量将恰好是 TableMapper.class 或 null。

另一个例子:

Class<Number> number = Integer.class; // does not compile
Number number = new Integer(1); // compiles fine
Class<? extends Number> number = Integer.class; // also ok

请注意,您可以使用 Class 执行不同的操作,然后使用 Class。例如,您可以调用 Class 的构造函数,因为您知道该类。你不能用 Class< 来做到这一点吗? extends Number> 因为构造函数不是在编译时定义的。

类似地,ListList。您可以对 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:

Class<? extends TableMapper> tableMapperClass = null;

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:

Class<Number> number = Integer.class; // does not compile
Number number = new Integer(1); // compiles fine
Class<? extends Number> number = Integer.class; // also ok

Note that you can do different things with Class<Number> then you can with Class<? extends Number>. For example, you can call a constructor of Class<Number>, because you know the class. You can't do that with Class<? extends Number> because the constructors are not defined at compile time.

Similarly with, say, List<Number> vs List<? extends Number>. You can call list.add(7) on a variable of type List<Number>, but you can't do that on a List<? extends Number> because you don't know the type of the second list. It might be a List<Double> for example, in which case adding an Integer is not allowed.

Generics are weird. :) Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文