基本上没有问题,但是还没有写全- import java.lang.reflect.Constructor;
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.Vector;
- public class Demo {
- /**
- * @param args
- * @throws SecurityException
- * @throws NoSuchMethodException
- * @throws InstantiationException
- * @throws InvocationTargetException
- * @throws IllegalArgumentException
- * @throws IllegalAccessException
- */
- public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
- // TODO Auto-generated method stub
- Class clazz = Person.class;
- Field[] fld = clazz.getFields();
-
- Method m = clazz.getMethod("speak", null);
-
- m.invoke(clazz.newInstance(), null);
- }
- }
- class Person{
- Person(){
- System.out.println("p");
- }
-
- public void speak(){
- System.out.println("speak");
- }
- }
复制代码 |