优化这段代码 if (CbmcDocTypeEnum.CREDIT.getName().equals(docType)) { docType = CbmcDocTypeEnum.CREDIT.getCode(); } else if (CertDocTypeEnum.CREDIT.getName().equals(docType)) { docType = CertDocTypeEnum.CREDIT.getCode(); } else if (CertDocTypeEnum.LABOURER_RIGHTS.getName().equals(docType)) { docType = CertDocTypeEnum.LABOURER_RIGHTS.getCode(); } else { fail += 1; result.append("第").append(i).append("条,产品类型格式有误!"); continue; }
时间: 2024-02-14 15:27:07 浏览: 75
javaandroid可用的ziprar解压缩代码实现.rar
你可以优化这段代码如下:
```java
String code = null;
if (CbmcDocTypeEnum.CREDIT.getName().equals(docType)) {
code = CbmcDocTypeEnum.CREDIT.getCode();
} else if (CertDocTypeEnum.CREDIT.getName().equals(docType)) {
code = CertDocTypeEnum.CREDIT.getCode();
} else if (CertDocTypeEnum.LABOURER_RIGHTS.getName().equals(docType)) {
code = CertDocTypeEnum.LABOURER_RIGHTS.getCode();
} else {
fail += 1;
result.append("第").append(i).append("条,产品类型格式有误!");
continue;
}
docType = code;
```
在这个优化后的代码中,我将重复的赋值语句提取出来,避免了重复的代码。使用一个局部变量`code`来存储根据`docType`获取到的对应的`code`值。最后再将`code`赋值给`docType`。这样可以使代码更简洁可读。
阅读全文