Java - 将类作为参数传递并使用它来调用函数

时间:2016-01-20 20:34:40

标签: java

有没有办法让一个类作为参数然后从该类变量调用一个函数?
(我不知道班级名称,但我知道函数名称)

public void executeFunction(RandomClass class1){
    class1.sayHi();
}

1 个答案:

答案 0 :(得分:0)

您可以使用反射来执行此操作。例如:

let crab_rate (pound, rate) = (float pound) * rate;
let lobster_rate (pound, rate) = (float pound) * rate;

let convert_func (crab_rate, lobster_rate)= function (first,second ) ->
 if crab_rate then (float pound) * rate;
 elif lobster_rate (float pound) * rate;


 let (crab_rate, lobster_rate) = convert_fun(3.4, 6.8); // $3.8 a pound for crab, $6.8 a pound for lobster.
// 10 being how many pounds i want.
crab_rate10 ;