我隔离的一段代码有问题。它返回给我一个 classCastException。 如果我评论这个块一切正常。 请问你能帮帮我吗? 谢谢
bankAccountService.listCustomers().forEach(customer -> {
try {
bankAccountService.saveCurrentBankAccount (Math.round(Math.random()*9000), 9000,customer.getId());
bankAccountService.saveSavingBankAccount (Math.round(Math.random()*100000), 3.6,customer.getId());
List<BankAccountDTO> bankAccounts= bankAccountService.bankAccountList();
for (BankAccountDTO bankAccount: bankAccounts){
for (int i = 0; i<10; i++){
try {
String accountId;
if(bankAccount instanceof SavingBankAccountDTO){
accountId=((SavingBankAccountDTO)bankAccount).getId();
}
else{
accountId=((CurrentBankAccountDTO)bankAccount).getId();
}
bankAccountService.credit(accountId,Math.round(1000+Math.random()*10000),"credit");
bankAccountService.debit(accountId,Math.round(Math.random()*1500),"debit" );
} catch (BankAccountNotFoundException | BalanceNotSufficentException e) {
throw new RuntimeException(e.getMessage());
}
}
};
} catch (CustomerNotFoundException e) {
throw new RuntimeException(e.getMessage().toUpperCase());
}
});
[![enter image description here](https://i.stack.imgur.com/AXx88.png)](https://i.stack.imgur.com/AXx88.png)
自从我施放了这个方块后,我遇到了一个问题
String accountId;
if(bankAccount instanceof SavingBankAccountDTO){
accountId=((SavingBankAccountDTO)bankAccount).getId();
}
else{
accountId=((CurrentBankAccountDTO)bankAccount).getId();
往来账户:
hi,` package es.riba.ebankingbackend.entities;
import jakarta.persistence.DiscriminatorValue;
import jakarta.persistence.Entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@DiscriminatorValue("CURR")
public class CurrentAccount extends BankAccount {
private double overDraft;
} `