package com.itheima.myreffect3;
import java.lang.reflect.Field;
public class ReflectDemo1 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
//mehtod1();
// method2();
// method3();
Class clazz = Class.forName("com.itheima.myreffect3.Student");
Field declaredField = clazz.getDeclaredField("money");
System.out.println(declaredField);
}
private static void method3() throws ClassNotFoundException, NoSuchFieldException {
Class clazz = Class.forName("com.itheima.myreffect3.Student");
Field field = clazz.getField("name");
System.out.println(field);
}
private static void method2() throws ClassNotFoundException {
Class clazz = Class.forName("com.itheima.myreffect3.Student");
Field[] declaredFields = clazz.getDeclaredFields();
for (Field declaredField : declaredFields) {
System.out.println(declaredField);
}
}
private static void mehtod1() throws ClassNotFoundException {
Class clazz = Class.forName("com.itheima.myreffect3.Student");
Field[] fields = clazz.getFields();
for (Field field : fields) {
System.out.println(field);
}
}
}
package com.itheima.myreffect3;
public class Student {
public String name;
public int age;
public String gernder;
private int money=300;
}
|