- 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[]{});
- }
- }
- }
复制代码 |