如何动态地从pojo中获取字段
how to get fields from a pojo dynamically
下面是我的 POJO
class,它有 50 个带有 setter 和 getter 的字段。
Class Employee{
int m1;
int m2;
int m3;
.
.
int m50;
//setters and getters
从我的另一个 class 我需要得到所有这 50 个字段才能得到它们的总和
Employee e1 =new Emploee();
int total = e1.getM1()+e2.getM2()+........e2.getM50();
有没有办法动态地(通过任何循环)执行此操作,而不是手动执行此操作以获取 50 条记录。
谢谢
是的,您可以像这样将它们放在一个数组中,而不是为每个 m1、m2、m3...设置一个单独的变量:
Class Employee {
public int[] m = new int[1000];
}
Employee e1 = new Employee();
int total = 0;
for(int i = 0; i < e1.m.length; i++) {
total += e1.m[i];
}
是的,不要使用 1000 个字段!使用包含 1000 个元素的数组,然后用 mi
填充 array[i-1]
您的 class 将类似于:
Class Employee{
int[] empArr = new int[1000];
}
然后使用可以找到这样的总和:
int sum = 0;
for(int i = 0; i<1000 ; i++)
sum+= e1.empArr[i]
我无法想象在 class 中有 1000 个字段的现实生活场景。话虽如此,您可以反射性地调用所有 getter。使用Introspector来完成这个任务:
int getEmployeeSum(Employee employee)
{
int sum = 0;
for(PropertyDescriptor propertyDescriptor : Introspector.getBeanInfo(Employee.class).getPropertyDescriptors())
{
sum += propertyDescriptor.getReadMethod().invoke(employee);
}
return sum;
}
您可以使用 java 反射。为简单起见,我假设您的 Employee
calss 仅包含 int
字段。但是您可以使用此处使用的类似规则来获取 float
、double
或 long
值。这是完整的代码 -
import java.lang.reflect.Field;
import java.util.List;
class Employee{
private int m=10;
private int n=20;
private int o=25;
private int p=30;
private int q=40;
}
public class EmployeeTest{
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException{
int sum = 0;
Employee employee = new Employee();
Field[] allFields = employee.getClass().getDeclaredFields();
for (Field each : allFields) {
if(each.getType().toString().equals("int")){
Field field = employee.getClass().getDeclaredField(each.getName());
field.setAccessible(true);
Object value = field.get(employee);
Integer i = (Integer) value;
sum = sum+i;
}
}
System.out.println("Sum :" +sum);
}
}
下面是我的 POJO
class,它有 50 个带有 setter 和 getter 的字段。
Class Employee{
int m1;
int m2;
int m3;
.
.
int m50;
//setters and getters
从我的另一个 class 我需要得到所有这 50 个字段才能得到它们的总和
Employee e1 =new Emploee();
int total = e1.getM1()+e2.getM2()+........e2.getM50();
有没有办法动态地(通过任何循环)执行此操作,而不是手动执行此操作以获取 50 条记录。
谢谢
是的,您可以像这样将它们放在一个数组中,而不是为每个 m1、m2、m3...设置一个单独的变量:
Class Employee {
public int[] m = new int[1000];
}
Employee e1 = new Employee();
int total = 0;
for(int i = 0; i < e1.m.length; i++) {
total += e1.m[i];
}
是的,不要使用 1000 个字段!使用包含 1000 个元素的数组,然后用 mi
填充 array[i-1]
您的 class 将类似于:
Class Employee{
int[] empArr = new int[1000];
}
然后使用可以找到这样的总和:
int sum = 0;
for(int i = 0; i<1000 ; i++)
sum+= e1.empArr[i]
我无法想象在 class 中有 1000 个字段的现实生活场景。话虽如此,您可以反射性地调用所有 getter。使用Introspector来完成这个任务:
int getEmployeeSum(Employee employee)
{
int sum = 0;
for(PropertyDescriptor propertyDescriptor : Introspector.getBeanInfo(Employee.class).getPropertyDescriptors())
{
sum += propertyDescriptor.getReadMethod().invoke(employee);
}
return sum;
}
您可以使用 java 反射。为简单起见,我假设您的 Employee
calss 仅包含 int
字段。但是您可以使用此处使用的类似规则来获取 float
、double
或 long
值。这是完整的代码 -
import java.lang.reflect.Field;
import java.util.List;
class Employee{
private int m=10;
private int n=20;
private int o=25;
private int p=30;
private int q=40;
}
public class EmployeeTest{
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException{
int sum = 0;
Employee employee = new Employee();
Field[] allFields = employee.getClass().getDeclaredFields();
for (Field each : allFields) {
if(each.getType().toString().equals("int")){
Field field = employee.getClass().getDeclaredField(each.getName());
field.setAccessible(true);
Object value = field.get(employee);
Integer i = (Integer) value;
sum = sum+i;
}
}
System.out.println("Sum :" +sum);
}
}