黑马程序员技术交流社区
标题:
Java如何使用反射调用父类的方法
[打印本页]
作者:
何小红
时间:
2012-9-21 22:08
标题:
Java如何使用反射调用父类的方法
本帖最后由 何小红 于 2012-9-21 22:10 编辑
Java如何使用反射调用父类的方法
求各位帮我看看
作者:
程振
时间:
2012-9-21 23:35
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
class Father{
public void fromFather(){
System.out.println("Method of Father");
}
}
class Son extends Father{
}
public class ReflectionDemo {
public static void main(String[] args) throws Exception {
Class sonClass = Son.class;
Class fatherClass = sonClass.getSuperclass();
Method[] method = fatherClass.getMethods();
for(Method tmp : method){
if(tmp.getName() == "fromFather")
tmp.invoke(fatherClass.newInstance(), new Object[]{});
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2