Java 获取Object中的某个List对象,并获取里面的值

 Java 获取Object对象中的某个List对象,并获取里面的值

 

package com.java;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class test1 {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        User user = new User(1,"张三");
        User user2 = new User(2,"李四");
        List users = new ArrayList<>();
        users.add(user);
        users.add(user2);
        Teacher teacher = new Teacher(1,users);
        System.out.println(teacher.toString());
        test1 test1 = new test1();
        test1.getUserList(teacher);

    }

    public void getUserList(Object object) throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Field field = object.getClass().getDeclaredField("users");
        field.setAccessible(true);
        Class clazz = field.get(object).getClass();
        Method m= clazz.getDeclaredMethod("size");
        int size = (Integer) m.invoke(field.get(object));
        for (int i = 0; i < size; i++) {//遍历list,调用get方法,获取list中的对象实例
            Method getM= clazz.getDeclaredMethod("get", int.class);
            if(!getM.isAccessible()){
                getM.setAccessible(true);
            }
            Object invoke = getM.invoke(field.get(object), i);
            Field id = invoke.getClass().getDeclaredField("id");
            Field username = invoke.getClass().getDeclaredField("username");
            id.setAccessible(true);
            username.setAccessible(true);
            System.out.println(id.get(invoke).toString());
            System.out.println(username.get(invoke).toString());
        }
    }
}




class Teacher{
    private int id;
    private List users;

    public Teacher() {
    }

    public Teacher(int id, List users) {
        this.id = id;
        this.users = users;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public List getUsers() {
        return users;
    }

    public void setUsers(List users) {
        this.users = users;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "id=" + id +
                ", users=" + users +
                '}';
    }
}

class User{
    private int id;
    private String username;

    public User(int id, String username) {
        this.id = id;
        this.username = username;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                '}';
    }
}

 

你可能感兴趣的:(java,java,反射,object)