当前位置: 代码迷 >> J2SE >> Java菜鸟,请问Java反射有关问题
  详细解决方案

Java菜鸟,请问Java反射有关问题

热度:79   发布时间:2016-04-23 20:45:44.0
Java初学者,请教Java反射问题
这是程序:
package com.hebrf.cla;

import java.lang.reflect.Method;

public class TestReplect{
public static void main(String[] args) {

 try { 
           Class cls = Class.forName("com.hebrf.cla.method"); 
           method c= new method();
           Method methlist[] = cls.getMethods(); 
           for(int i=0;i<methlist.length;i++){
            Class<?>[] types = methlist[i].getParameterTypes();
            System.out.println("mrthod:"+methlist[i].toString());
            for(int j=0;j<types.length;j++){
            System.out.println("type:"+types[j]);
            }
            if(types.length == 0){
            methlist[i].invoke(c);
            }else{
            methlist[i].invoke(c, 3);
            }
           }
       } 
       catch (Throwable e) { 
           System.err.println(e); 
       } 
}
}
为什么最后会报:mrthod:public final void java.lang.Object.wait() throws java.lang.InterruptedException
java.lang.reflect.InvocationTargetException这种错误,求指教
------解决方案--------------------
  Method methlist[] = cls.getMethods(); 
           改成
        Method methlist[] = cls.getDeclaredMethods();
  相关解决方案