Java反射获取对象属性并设值
来源:互联网 发布:数据库前置机 编辑:程序博客网 时间:2024/05/29 05:54
Java对象:
package com.sjdf.erp.common.utils;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); } else { System.out.println("不支持的数据类型:" + fieldName + " : " + fieldClassName); } } return t; }}
输出结果:
bigBoolean : BooleansmallBoolean : booleanbigLong : LongbigDouble : DoublebigInt : IntegerbigFloat : FloatsmallFloat : floatsmallLong : longsmallDouble : doublesmallInt : intdecimal : BigDecimalstring : StringTypeTest [bigBoolean=true, smallBoolean=true, bigLong=666666699999, bigDouble=10.2, bigInt=10, bigFloat=127.6, smallFloat=127.6, smallLong=666666699999, smallDouble=10.2, smallInt=10, decimal=1238.502, string=你好]
阅读全文
0 0
- Java反射获取对象属性并设值
- java反射动态给对象属性赋值,并获取属性值
- java反射获取对象的属性及属性值
- js获取点击对象并设值
- 使用反射获取父类的属性并设值,适用于类中没有提供setter方法
- Java通过反射获取和设置对象的属性值
- java反射获取当前对象属性的值
- java反射应用-获取对象自身的属性和值
- 反射获取对象属性
- java反射获取属性值
- java发射机制获取属性,给属性设值
- 反射获取对象的属性值
- C#利用反射获取对象属性值
- C#利用反射获取对象属性值
- 反射获取对象的属性名和对应的值并转为json字符串
- 通过java反射机制来获取属性的类型,获取属性的值并给属性赋值
- java获取反射获取私有属性值
- java反射之Field 获取对象属性及设置属性值
- 软件测试分类
- “==”与equals的区别(个人理解)
- FAFU OJ 求1到n和
- 好记性不如烂笔头
- nova vnc proxy基本原理
- Java反射获取对象属性并设值
- 如何解决NullPointerException in android.view.GLES20RecordingCanvas
- opencv for python + 树莓派摄像头 常见错误
- 常用的排序算法-java
- oracle游标详解
- 入门级产品级产品经理工作中常见问题整理
- 定义工厂(Plant)
- 第四章 INI配置——跟我学习springmvc shiro mybatis
- 结果集映射,一些标签的用法