- import java.lang.reflect.Field;
- import part04.Man.Body;
- public class GetInnerClass {
- /**
- * @param args
- * @throws Exception
- * @throws NoSuchFieldException
- */
- public static void main(String[] args) throws Exception {
- // TODO Auto-generated method stub
- Class[] ca=Man.class.getDeclaredClasses();
- System.out.println(ca[0].getName());
- Field f1=ca[0].getDeclaredField("color");
-
- Body good = new Man(8, "zhangsan").new Body("black",true);
- f1.set(good, "write");
- System.out.println(f1.get(good));
-
- }
- }
- class Man{
- int age;
- public Man(int age, String name) {
- super();
- this.age = age;
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- String name;
-
- class Body{
- public Body(String color, boolean sex) {
- super();
- this.color = color;
- this.sex = sex;
- }
- String color;
- boolean sex=true;
- public String getColor() {
- return color;
- }
- public void setColor(String color) {
- this.color = color;
- }
- public boolean isSex() {
- return sex;
- }
- public void setSex(boolean sex) {
- this.sex = sex;
- }
- }
- }
复制代码 |