在java中将父类强制转换为子类

ltqd579y  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(370)

我有2节课。一个用于api请求,一个用于响应。我的工作是添加2个参数 error, result 发送到请求主体并在响应中发回。
我试过铸造,但失败了:

for (InteracReportingDetails item : interacReportingDetailsList) {  
            result1.add((InteracReportingDetailsResponse) item);
}

做这件事的简单/正确方法是什么?有没有办法避免给个别设定者打电话?
我应该换接口吗?克隆?
一级-api请求主体(spring):

public class InteracReportingDetails {

    private String interacReportingCode,interacReportingTypeCode,participantId, interacReferenceId, interacReferenceIdType, reportedByCode, transactionTimeStamp, interacReportingTimeStamp, memo;
    public String getParticipantId() {
        return participantId;
    }

    public void setParticipantId(String participantId) {
        this.participantId = participantId;
    }

    public String getInteracReportingCode() {
        return interacReportingCode;
    }

    public void setInteracReportingCode(String interacReportingCode) {
        this.interacReportingCode = interacReportingCode;
    }

    public String getInteracReportingTypeCode() {
        return interacReportingTypeCode;
    }

    public void setInteracReportingTypeCode(String interacReportingTypeCode) {
        this.interacReportingTypeCode = interacReportingTypeCode;
    }

    public String getInteracReferenceId() {
        return interacReferenceId;
    }

    public void setInteracReferenceId(String interacReferenceId) {
        this.interacReferenceId = interacReferenceId;
    }

    public String getInteracReferenceIdType() {
        return interacReferenceIdType;
    }

    public void setInteracReferenceIdType(String interacReferenceIdType) {
        this.interacReferenceIdType = interacReferenceIdType;
    }

    public String getReportedByCode() {
        return reportedByCode;
    }

    public void setReportedByCode(String reportedByCode) {
        this.reportedByCode = reportedByCode;
    }

    public String getTransactionTimeStamp() {
        return transactionTimeStamp;
    }

    public void setTransactionTimeStamp(String transactionTimeStamp) {
        this.transactionTimeStamp = transactionTimeStamp;
    }

    public String getMemo() {
        return memo;
    }

    public void setMemo(String memo) {
        this.memo = memo;
    }

    public String getInteracReportingTimeStamp() {
        return interacReportingTimeStamp;
    }

    public void setInteracReportingTimeStamp(String interacReportingTimeStamp) {
        this.interacReportingTimeStamp = interacReportingTimeStamp;
    }
}

二等舱:

public class InteracReportingDetailsResponse extends InteracReportingDetails {

    private String error, result;

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }
}
dluptydi

dluptydi1#

我用以下方法解决了这个问题:

import org.springframework.beans.BeanUtils;
BeanUtils.copyProperties(source, target);

相关问题