繁体   English   中英

通过反射调用方法时出错

[英]Error while invoking method through reflection

以下是我使用反射调用的方法的两种情况

actualoutput = mgenerateouput.invoke(outputclassinst,obj); 

objObject数组类型,其中包含obj[0]中的Section Type对象

情况1:

public Student[] expectedOutputString(Section sec){
        //Object arra[] = Section.makeSection((String[])params[0]);
        ReportCard rc = new ReportCard();
        Student[] exOut = rc.orderClass(sec);
        return exOut;
    }

它在第一种情况下工作完美,但在下面的第二种情况下,当我将参数类型声明为Object我得到了IllegalArgumentException

情况2:

public Student[] expectedOutputString(Object params[]){
    //Object arra[] = Section.makeSection((String[])params[0]);
    ReportCard rc = new ReportCard();
    Student[] exOut = rc.orderClass((Section)params[0]);
    return exOut;
}

[

可能是拼写错误:

rc.orderClass((Section)params[0]);  // was: Second
               ^^^^^^^

代码2并不将参数声明为Object,而是声明为Object [],即对象数组。 因此,您的obj [0]也必须是对象数组。 编辑:或者反过来:代码2应该期望使用Object而不是Object []。

为什么在案例2中不使用可变参数? 喜欢:

public Student[] expectedOutputString(Object ... params){

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM