Skip to content

Commit 98fef09

Browse files
author
yongjie.zhang
committed
invoke
1 parent 4474515 commit 98fef09

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

java-reflect/src/com/zyj/reflect/Demo02.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.zyj.reflect;
22

3+
import com.zyj.bean.User;
4+
35
import java.lang.reflect.Constructor;
46
import java.lang.reflect.Field;
7+
import java.lang.reflect.InvocationTargetException;
58
import java.lang.reflect.Method;
69

710
/**
@@ -46,7 +49,13 @@ public static void main(String[] args) throws NoSuchFieldException, NoSuchMethod
4649
Constructor a = clazz.getDeclaredConstructor(int.class,int.class,String.class);
4750
System.out.println("获得的构造器"+a);
4851
System.out.println(setUname);
49-
} catch (ClassNotFoundException e) {
52+
User user = (User)clazz.newInstance();
53+
setUname.invoke(user,"zyj");
54+
Method getUname = clazz.getMethod("getName");
55+
System.out.println("666"+getUname.invoke(user));
56+
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
57+
e.printStackTrace();
58+
} catch (InvocationTargetException e) {
5059
e.printStackTrace();
5160
}
5261
}

0 commit comments

Comments
 (0)