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

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")) {
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值