为什么我的 public void Constructor {} 不能编译? [英] Why won't my public void Constructor {} compile?

查看:26
本文介绍了为什么我的 public void Constructor {} 不能编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项任务,需要一个银行账户才能从支票和储蓄账户中转移资金.交易存储在一个 ArrayList 中,并为用户设置以指定何时转移资金.用于支票和储蓄的银行帐户类工作正常,但我创建的 TransferService 类在 NetBeans 中没有正确编译.

I have an assignment that requires a bank account to be able to transfer funds from a checking and savings account. The transactions are stored in an ArrayList and set up for the user to specify when to transfer the funds. The bank account class for checking and savings works OK, but the TransferService class I created isn't compiling properly in NetBeans.

提示似乎没有修复错误.我收到错误:

The hints don't seem to be fixing the errors. I get the error:

事务是抽象的,不能被实例化.

Transaction is abstract and cannot be instantiated.

我该如何解决这个问题?

How can I fix this problem?

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");
        }
    }

推荐答案

构造函数没有返回类型.所以不是

Constructors have no return type. So not

// this is a "pseudo"-constructor
public void TransferService(){

而是

// this is the real deal
public TransferService(){

<小时>

关于,

事务是抽象的,无法实例化

Transaction is abstract and cannot be instantiated

嗯,是吗?Transaction 类是抽象类还是接口?只有拥有代码的您才知道答案.如果这是真的,那么您需要在代码中使用具体的 Transaction 实现.

Well, is it? Is the Transaction class an abstract class or an interface? Only you who has the code knows the answer to this. If this is true, then you'll need to use a concrete implementation of Transaction in your code.

这篇关于为什么我的 public void Constructor {} 不能编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆