- import java.lang.reflect.Constructor;
- import java.lang.reflect.Field;
- public class ReflectTest {
- public static void main(String[] args) throws ClassNotFoundException, Exception {
- Class s1= Class.forName("com.itheima.Student");
- Constructor c1= s1.getConstructor(String.class,int.class);
- Student st1=(Student)c1.newInstance("wanggang",23);
- Field f1= s1.getDeclaredField("age");
-
- f1.set(st1,55) ;
-
- System.out.println(st1.getAge());
- }
- }
复制代码
|
|