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

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

问题描述

我有一个作业,要求一个银行帐户能够从支票和储蓄帐户转帐资金.交易存储在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.

如何解决此问题?

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(){


关于


Regarding,

交易是抽象的,无法实例化

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.

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

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