先获取Method对象
以下仅供参考
package com.kidd.test.zhidao;
import java.lang.reflect.Method;
/**
* Hello world!
*
*/
public class Main {
public static void main(String[] args) {
Method method1 = null;
Method method2 = null;
try {
method1 = Class.forName("com.kidd.test.zhidao.Cat").getMethod("getName", (Class>[]) null);
method2 = Class.forName("com.kidd.test.zhidao.Cat").getMethod("getChilds", (Class>[]) null);
} catch (NoSuchMethodException ex) {
ex.printStackTrace();
} catch (SecurityException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
if (null != method1) {
System.out.println(method1.getGenericReturnType().getTypeName());
}
if (null != method2) {
System.out.println(method2.getGenericReturnType().getTypeName());
}
}
}
class Cat {
private String name;
private Cat[] childs;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Cat[] getChilds() {
return childs;
}
public void setChilds(Cat[] childs) {
this.childs = childs;
}
}
不是类型,Class在java.lang 同过不同的方法(4种)来动态的获取你所需要的类或接口