java—类可以是方法的参数吗?

jk9hmnmh  于 2021-06-03  发布在  Hadoop
关注(0)|答案(2)|浏览(310)

几天以来,我一直在尝试理解hadoopmapreduce程序。我看到下面的陈述。

conf.setInputFormat(TextInputFormat.class);

我不质疑这个声明的合法性,因为程序运行没有问题。有人能解释一下为什么textinputformat.class是输入而不是text input format类型的对象吗?我也可以用同样的约定来处理其他方法吗?在什么情况下会失败?
这是setinputformat的签名。

<http://hadoop.apache.org/docs/current/api/org/apache/hadoop/mapred/JobConf.html#setInputFormat(java.lang.Class)>

setInputFormat(Class<? extends InputFormat> theClass) 

Set the InputFormat implementation for the map-reduce job.
v1uwarro

v1uwarro1#

Map器和还原器需要知道什么样的格式。他们不需要真正的控制。每个Map器/还原器都将使用它 Class 使用反射来示例化他们自己。给别人一把锤子和告诉别人用锤子是有区别的。你的例子是使用锤子的说明。

t2a7ltrp

t2a7ltrp2#

是的,您可以将类作为参数传递。下面是如何将类作为参数传递的简短示例:

public void foo(Class obj){
    Object ob = obj.newInstance();
}

相关问题