Java初学者,请教Java反射问题

天辰北 2014-05-09 10:41:23
这是程序:
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这种错误,求指教
...全文
123 2 打赏 收藏 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Magical茏 2014-05-10
  • 打赏
  • 举报
回复
// 获取所有的公有方法,包含父类的.
		Method[] methods = clazz.getMethods();
		// 获取所有的方法.包含私有的.
		Method[] declaredMethods = clazz.getDeclaredMethods();
楼主的method类 应该首字母大写,养成良好的命名习惯十分必要.
lymoge 2014-05-09
  • 打赏
  • 举报
回复
Method methlist[] = cls.getMethods(); 改成 Method methlist[] = cls.getDeclaredMethods();

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧