Java动态调用类的方法案例

一、场景

需要根据编号动态给类设置值,属性名为“f+费用编号”,例如f01表示编号为01的费用

public class MonitorDayFee implements Serializable {
    private static final long serialVersionUID = 1L;

    private Long dayFeeId;//主键

    private String ordinalNo;//流水号

    private Integer collectYear;//统计年份

    private Integer collectMonth;//统计月份

    private Integer collectDay;//统计天

    private String deptId;//院科室ID

    private String deptName;//院科室名称

    private BigDecimal f01;//床位费

    private BigDecimal f02;//西药费

    private BigDecimal f03;//中药费

    private BigDecimal f04;//中成药

    private BigDecimal f05;//中草药

    private BigDecimal f06;//检查费

    private BigDecimal f07;//治疗费

    private BigDecimal f08;//放射费

    private BigDecimal f09;//手术费

    private BigDecimal f10;//化验费

    private BigDecimal f11;//输血费

    private BigDecimal f12;//输氧费

    private BigDecimal f13;//其它

    private BigDecimal f14;//麻醉费

    private BigDecimal f15;//材料费

    private BigDecimal f16;//特殊检查费

    private BigDecimal f17;//特殊治疗费

    public Long getDayFeeId() {
        return dayFeeId;
    }

    public void setDayFeeId(Long dayFeeId) {
        this.dayFeeId = dayFeeId;
    }

    public String getOrdinalNo() {
        return ordinalNo;
    }

    public void setOrdinalNo(String ordinalNo) {
        this.ordinalNo = ordinalNo;
    }

    public Integer getCollectYear() {
        return collectYear;
    }

    public void setCollectYear(Integer collectYear) {
        this.collectYear = collectYear;
    }

    public Integer getCollectMonth() {
        return collectMonth;
    }

    public void setCollectMonth(Integer collectMonth) {
        this.collectMonth = collectMonth;
    }

    public Integer getCollectDay() {
        return collectDay;
    }

    public void setCollectDay(Integer collectDay) {
        this.collectDay = collectDay;
    }

    public String getDeptId() {
        return deptId;
    }

    public void setDeptId(String deptId) {
        this.deptId = deptId;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    public BigDecimal getF01() {
        return f01;
    }

    public void setF01(BigDecimal f01) {
        this.f01 = f01;
    }

    public BigDecimal getF02() {
        return f02;
    }

    public void setF02(BigDecimal f02) {
        this.f02 = f02;
    }

    public BigDecimal getF03() {
        return f03;
    }

    public void setF03(BigDecimal f03) {
        this.f03 = f03;
    }

    public BigDecimal getF04() {
        return f04;
    }

    public void setF04(BigDecimal f04) {
        this.f04 = f04;
    }

    public BigDecimal getF05() {
        return f05;
    }

    public void setF05(BigDecimal f05) {
        this.f05 = f05;
    }

    public BigDecimal getF06() {
        return f06;
    }

    public void setF06(BigDecimal f06) {
        this.f06 = f06;
    }

    public BigDecimal getF07() {
        return f07;
    }

    public void setF07(BigDecimal f07) {
        this.f07 = f07;
    }

    public BigDecimal getF08() {
        return f08;
    }

    public void setF08(BigDecimal f08) {
        this.f08 = f08;
    }

    public BigDecimal getF09() {
        return f09;
    }

    public void setF09(BigDecimal f09) {
        this.f09 = f09;
    }

    public BigDecimal getF10() {
        return f10;
    }

    public void setF10(BigDecimal f10) {
        this.f10 = f10;
    }

    public BigDecimal getF11() {
        return f11;
    }

    public void setF11(BigDecimal f11) {
        this.f11 = f11;
    }

    public BigDecimal getF12() {
        return f12;
    }

    public void setF12(BigDecimal f12) {
        this.f12 = f12;
    }

    public BigDecimal getF13() {
        return f13;
    }

    public void setF13(BigDecimal f13) {
        this.f13 = f13;
    }

    public BigDecimal getF14() {
        return f14;
    }

    public void setF14(BigDecimal f14) {
        this.f14 = f14;
    }

    public BigDecimal getF15() {
        return f15;
    }

    public void setF15(BigDecimal f15) {
        this.f15 = f15;
    }

    public BigDecimal getF16() {
        return f16;
    }

    public void setF16(BigDecimal f16) {
        this.f16 = f16;
    }

    public BigDecimal getF17() {
        return f17;
    }

    public void setF17(BigDecimal f17) {
        this.f17 = f17;
    }
}

二、计算值动态设置进对象

获取编号拼接方法名“setF+编号”

 monitorDayFee=this.addMonitorDayFeeItemFee(monitorDayFee,"F"+itemType.getItemTypeCode(),itemFee);

通过反射机制动态调用

   private MonitorDayFee addMonitorDayFeeItemFee(MonitorDayFee monitorDayFee, String itemCode,BigDecimal itemFee) throws Exception{
        Class classZ=Class.forName("com.zhq.business.scheduled.model.MonitorDayFee");
        Method method=classZ.getMethod("set"+itemCode,BigDecimal.class);
        method.invoke(monitorDayFee, itemFee);
        return monitorDayFee;
    }

 

你可能感兴趣的:(菜鸟JAVA之旅)