public void setStrDay(String strDay) { this.strDay = strDay; } public void setStrMonth(String strMonth) { this.strMonth = strMonth; } public void setStrYear(String strYear) { this.strYear = strYear; } }public class TestMethod { public static void main(String[] args) { try { Class cl = Class.forName("MethodClass"); test(cl); } catch(Exception ex) { ex.printStackTrace(); } } // 例如我们要遍历一个名叫customer的类,那么我们写一个遍历这个类的函数 public static Object test(Class type) throws Exception { Method[] method = type.getDeclaredMethods(); //取得该类的所有方法 for (int i = 0; i < method.length; i++) { Method s_method = method[i]; String method_name = s_method.getName(); //取得该方法的名 if (method_name.length() >= 3 && method_name.substring(0, 3).equals("get")) { //如果该方法名以get开头 Object get_value = s_method.invoke(type.newInstance(), null); //运行该方法,其中后面一个参数null是需要传进该方法的参数,是Object[]类型的,运行该方法后返回的也是Object类型,如果你需要返回字符串类型,可以再进行转换 return get_value; } } return null; } }
依葫芦画瓢,加在get前 for (int i = 0; i < method.length; i++) { Method s_method = method[i]; String method_name = s_method.getName(); //取得该方法的名 if (method_name.length() >= 3 && method_name.substring(0, 3).equals("set")) { s_method.invoke(type.newInstance(), new Object[]{"String"+i}); } }
编译没问题,但运行时报错。晕死了。请再帮忙看下。多谢。java.lang.ClassNotFoundException: MethodClass at java.net.URLClassLoader$1.run(URLClassLoader.java:199) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:187) at java.lang.ClassLoader.loadClass(ClassLoader.java:289) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274) at java.lang.ClassLoader.loadClass(ClassLoader.java:235) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:141) at com.hy.test.TestMethod.main(TestMethod.java:50)
getMethods();
如果需要所有方法,包括private
就用getDelcaredMethods()不过只获得定义在该类的方法,不包括继承来的
例如我们要遍历一个名叫customer的类,那么我们写一个遍历这个类的函数
public static Object test(Class type) throws Exception
{
Method[] method = type.getDeclaredMethods();//取得该类的所有方法
for (int i = 0; i < method.length; i++)
{
Method s_method = method[i];
String method_name = s_method.getName();//取得该方法的名
if (method_name.length() >=3 && method_name.substring(0,3).equals("get"))//如果该方法名以get开头
{
Object get_value = s_method.invoke(type.newInstance(), null);//运行该方法,其中后面一个参数null是需要传进该方法的参数,是Object[]类型的,运行该方法后返回的也是Object类型,如果你需要返回字符串类型,可以再进行转换
}
}
}
test(cl);
哪里说了可以直接获得所有方法????
获得的是类型中声明的方法
Mehtod[] getMethods()获得一个类的所有的public类型的方法
Mehtod getDeclaredMethod(String name, Class[] params)使用指定的参数类型获得由name参数所指定的由这个类声明的方法。
Method[] getDeclaredMethods() 获得这个类所声明的所有的方法Constructor getConstructor(Class[] params) 使用指定的参数类型来获得公共的构造器;
Constructor[] getConstructors() 获得这个类的所有构造器;
Constructor getDeclaredConstructor(Class[] params) 使用指定的参数类型来获得构造器(忽略访问的级别)
Constructor[] getDeclaredConstructors() 获得这个类的所有的构造器(忽略访问的级别)
再遍历进去,就能得到所有的了
下面是我的程序。还有个问题就是,我要先set一下,要不打get是就没有值,怎么做。
搞定了另加分。
import java.lang.Class;
import java.lang.reflect.Method;class MethodClass
{
private String strMonth;
private String strYear;
private String strDay;
public String getStrYear()
{
return strYear;
} public String getStrMonth()
{
return strMonth;
} public String getStrDay()
{
return strDay;
}
public void setStrDay(String strDay)
{
this.strDay = strDay;
} public void setStrMonth(String strMonth)
{
this.strMonth = strMonth;
} public void setStrYear(String strYear)
{
this.strYear = strYear;
}
}public class TestMethod
{
public static void main(String[] args)
{
try
{
Class cl = Class.forName("MethodClass");
test(cl);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
// 例如我们要遍历一个名叫customer的类,那么我们写一个遍历这个类的函数
public static Object test(Class type) throws Exception
{
Method[] method = type.getDeclaredMethods(); //取得该类的所有方法
for (int i = 0; i < method.length; i++)
{
Method s_method = method[i];
String method_name = s_method.getName(); //取得该方法的名
if (method_name.length() >= 3 &&
method_name.substring(0, 3).equals("get"))
{ //如果该方法名以get开头
Object get_value = s_method.invoke(type.newInstance(), null); //运行该方法,其中后面一个参数null是需要传进该方法的参数,是Object[]类型的,运行该方法后返回的也是Object类型,如果你需要返回字符串类型,可以再进行转换 return get_value;
}
}
return null;
}
}
{
Method s_method = method[i];
String method_name = s_method.getName(); //取得该方法的名
if (method_name.length() >= 3 &&
method_name.substring(0, 3).equals("set"))
{
s_method.invoke(type.newInstance(), new Object[]{"String"+i});
}
}
我的两个类都在同一个文件中定义的。上面全贴出来了。
小弟学JAVA刚两个月,好多东西不懂啊,上班又不能上网。所以。
请帮忙调一下。
没有人会怀疑你的英语水平,OK?
况且junit不是eclipse的东西!谁在前?