使用反射能不能实现 假如有一个方法:public List a(Object obj){ } a 方法要实现多态,也就是说可以接收任何类型的对象,我想在方法体里面知道对象的各种方法,使用反射能不能实现? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public List a(Object obj){ Class c = obj.getClass(); Method[] method = c.getMethods(); } package com.tutorinfo.forum;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.List;public class GetMethod { /* * 返回参数中的对象的类的全部方法名 */ @SuppressWarnings("unchecked") public <T> List<String> a(T t){ List<String> list=new ArrayList(); if (null!=t){ Class clazz=t.getClass(); //对象反映此 Class 对象表示的类或接口声明的所有方法,包括公共、保护、默认(包)访问和私有方法,但不包括继承的方法 //Method[] methods=clazz.getDeclaredMethods(); //返回一个包含某些 Method 对象的数组,这些对象反映此 Class 对象所表示的类或接口(包括那些由该类或接口声明的以及从超类和超接口继承的那些的类或接口)的公共 member 方法。 Method[] methods=clazz.getMethods(); for(Method m:methods){ list.add(m.toString()); } } return list; } public static void main(String[] args){ GetMethod gm=new GetMethod(); List<String> methods=gm.a(gm); //测试一个对象,获取它全部的方法名 System.out.println(methods); }} 你看看反射中 method类的invoke方法 method[index].invoke 方法需要的参数穿进去就可以 一道题目 Java中为什么不能在声明数组大小的同时初始化 JTextField的事件监听功能 接口的方法都实了,还出错,说什么没有实现,可我都实现了啊 求助GridBagLayout的各种参数设置方法 if内声明的变量离开if就没了吧? 初学的一些疑问,希望各位大侠热心帮忙噢 这样怎么一点显示都没有? 请问怎么用ant编译java?急!在线等待 java新手!!关于用protected修饰的成员内部类的继承问题!!求大神指教!! List<Integer>转int[] 希望高手帮忙写一个正则表达式
public List a(Object obj){
Class c = obj.getClass();
Method[] method = c.getMethods();
}
package com.tutorinfo.forum;import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;public class GetMethod {
/*
* 返回参数中的对象的类的全部方法名
*/
@SuppressWarnings("unchecked")
public <T> List<String> a(T t){
List<String> list=new ArrayList();
if (null!=t){
Class clazz=t.getClass();
//对象反映此 Class 对象表示的类或接口声明的所有方法,包括公共、保护、默认(包)访问和私有方法,但不包括继承的方法
//Method[] methods=clazz.getDeclaredMethods();
//返回一个包含某些 Method 对象的数组,这些对象反映此 Class 对象所表示的类或接口(包括那些由该类或接口声明的以及从超类和超接口继承的那些的类或接口)的公共 member 方法。
Method[] methods=clazz.getMethods();
for(Method m:methods){
list.add(m.toString());
}
}
return list;
}
public static void main(String[] args){
GetMethod gm=new GetMethod();
List<String> methods=gm.a(gm); //测试一个对象,获取它全部的方法名
System.out.println(methods);
}}
你看看反射中 method类的invoke方法