Java反射获取对象属性并设值

15 阅读1分钟

import java.math.BigDecimal;

public class TypeTest {

private Boolean bigBoolean;

private Long bigLong;

private Double bigDouble;

private Integer bigInt;

private Float bigFloat;

private boolean smallBoolean;

private float smallFloat;

private long smallLong;

private double smallDouble;

private int smallInt;

private BigDecimal decimal;

private String string;



public Long getBigLong() {

    return bigLong;

}

public Double getBigDouble() {

    return bigDouble;

}

public Integer getBigInt() {

    return bigInt;

}

public Float getBigFloat() {

    return bigFloat;

}

public float getSmallFloat() {

    return smallFloat;

}

public long getSmallLong() {

    return smallLong;

}

public double getSmallDouble() {

    return smallDouble;

}

public int getSmallInt() {

    return smallInt;

}

public BigDecimal getDecimal() {

    return decimal;

}

public String getString() {

    return string;

}

public void setBigLong(Long bigLong) {

    this.bigLong = bigLong;

}

public void setBigDouble(Double bigDouble) {

    this.bigDouble = bigDouble;

}

public void setBigInt(Integer bigInt) {

    this.bigInt = bigInt;

}

public void setBigFloat(Float bigFloat) {

    this.bigFloat = bigFloat;

}

public void setSmallFloat(float smallFloat) {

    this.smallFloat = smallFloat;

}

public void setSmallLong(long smallLong) {

    this.smallLong = smallLong;

}

public void setSmallDouble(double smallDouble) {

    this.smallDouble = smallDouble;

}

public void setSmallInt(int smallInt) {

    this.smallInt = smallInt;

}

public void setDecimal(BigDecimal decimal) {

    this.decimal = decimal;

}

public void setString(String string) {

    this.string = string;

}

public Boolean getBigBoolean() {

    return bigBoolean;

}

public boolean isSmallBoolean() {

    return smallBoolean;

}

public void setBigBoolean(Boolean bigBoolean) {

    this.bigBoolean = bigBoolean;

}

public void setSmallBoolean(boolean smallBoolean) {

    this.smallBoolean = smallBoolean;

}

@Override

public String toString() {

    return "TypeTest [bigBoolean=" + bigBoolean + ", smallBoolean="

            + smallBoolean + ", bigLong=" + bigLong + ", bigDouble="

            + bigDouble + ", bigInt=" + bigInt + ", bigFloat=" + bigFloat

            + ", smallFloat=" + smallFloat + ", smallLong=" + smallLong

            + ", smallDouble=" + smallDouble + ", smallInt=" + smallInt

            + ", decimal=" + decimal + ", string=" + string + "]";

}

}




  

反射设值:



package com.sjdf.erp.common.utils;

import java.lang.reflect.Field;

import java.math.BigDecimal;

public class Test {

public static void main(String[] args) throws Exception {

    TypeTest test = getBean(TypeTest.class);

    System.out.println(test.toString());

}



/**

 * 获取单个Bean

 * @param c

 * @param obj

 * @return

 * @throws Exception

 */

private static <T> T getBean(Class<T> c) throws Exception{

    T t = c.newInstance();

    Field[] fields = c.getDeclaredFields();

    for (int i = 0; i < fields.length; i++) {

        Field field = fields[i];

        String fieldName = field.getName();

        // 获取具体值

        field.setAccessible(true);

        // 其余情况根据类型赋值

        String fieldClassName = field.getType().getSimpleName();

        System.out.println(fieldName + " : " + fieldClassName);

        if (fieldClassName.equalsIgnoreCase("String")) {

            field.set(t, "你好");

        } else if (fieldClassName.equalsIgnoreCase("boolean")) {

            field.set(t, true);

        } else if (fieldClassName.equalsIgnoreCase("int") || fieldClassName.equals("Integer")) {

            field.set(t, 10);

        } else if (fieldClassName.equalsIgnoreCase("double")) {

            field.set(t, 10.2);

        } else if (fieldClassName.equalsIgnoreCase("long")) {

            field.set(t, 666666699999L);

        } else if (fieldClassName.equalsIgnoreCase("BigDecimal")) {

            field.set(t, new BigDecimal("1238.502"));

        }else if (fieldClassName.equalsIgnoreCase("float")) {

            field.set(t, 127.6F);