Java构造函数没有正确编译 [英] Java constructor not compiling properly

查看:179
本文介绍了Java构造函数没有正确编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手,最近刚开始上课,所以仍然掌握一切是如何运作的,所以请在我尝试理解所有这些新材料时请耐心等待。

I'm a newbie to Java and just started classes recently so still getting the hang of how everything works in it so please bear with me while I try to comprehend all this new material.

我的任务要求银行账户能够从支票和储蓄账户转账。交易存储在arraylist中并为用户设置以指定何时转移资金。用于检查和保存的银行帐户类工作正常但我创建的transferervice类在我正在使用的ide 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 arraylist and setup 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 my ide netbeans I'm using.

我的教师重新编写了一些代码以帮助它,但它仍然无法编译,提示似乎没有修复错误。我得到的Transaction是抽象的,无法实例化。我不太清楚我能做些什么来解决这个错误,所以任何帮助都会非常感激。

My instructor reworked some of my code to help but it still won't compile and the hints don't seem to be fixing the errors. I get Transaction is abstract and cannot be instantiated. Not quite sure what I can do to fix that error so any help would be greatly appreciated.

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.

这篇关于Java构造函数没有正确编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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