MrL*_*l81 2 java constructor compiler-errors banking
我有一项作业,要求银行帐户能够从支票和储蓄帐户转移资金。交易存储在 ArrayList 中,并由用户指定何时转移资金。用于支票和储蓄的银行帐户类工作正常,但我创建的 TransferService 类在 NetBeans 中无法正确编译。
这些提示似乎并没有修复错误。我收到错误:
事务是抽象的,无法实例化。
我该如何解决这个问题?
import java.util.ArrayList;
import java.util.Date;
import javax.transaction.Transaction;
public class TransferService {
private Date currentDate;
private ArrayList<Transaction> completedTransactions;
private ArrayList<Transaction> pendingTransactions;
public void TransferService(){
this.currentDate = new Date();
this.completedTransactions = new ArrayList<Transaction>();
this.pendingTransactions = new ArrayList<Transaction>();
}
public TransferService(BankAccount to, BankAccount from, double amount, Date when) throws InsufficientFundsException(){
if (currentDate.after(when)){
try(
from.withdrawal(amount);
to.deposit(amount);
completedTransactions.add(new Transaction(to, from, this.currentDate, Transaction.TransactionStatus.COMPLETE));
} catch (InsufficientFundsException ex){
throw ex;
}
} else {
pendingTransactions.add(new Transaction(to, from, null, Transaction.TransactionStatus.PENDING));
}
}
private static class InsufficientFundsException extends Exception {
public InsufficientFundsException() {
System.out.println("Insufficient funds for transaction");
}
}
Run Code Online (Sandbox Code Playgroud)
构造函数没有返回类型。所以不
// this is a "pseudo"-constructor
public void TransferService(){
Run Code Online (Sandbox Code Playgroud)
反而
// this is the real deal
public TransferService(){
Run Code Online (Sandbox Code Playgroud)
关于,
事务是抽象的,无法实例化
嗯,是吗?Transaction类是抽象类还是接口?只有拥有代码的你才知道这个问题的答案。如果这是真的,那么您将需要在代码中使用 Transaction 的具体实现。