繁体   English   中英

我无法弄清楚此错误消息(ArrayList BankAccount代码)

[英]I can't figure out this error message (ArrayList BankAccount code)

我想明天尝试用Java进行测试,但是我不知道发生了什么。

import java.util.ArrayList;
public class BankAccount
{
    public static void BankAccount(double x)
    {
        ArrayList<BankAccount> accounts = new ArrayList<BankAccount>();
        BankAccount ted = new BankAccount(400);
        BankAcconut bob = new BankAccount(300);
        BackAccount carol = new BankAccount(500);
        accounts.add(new BankAccount(300));
    }
}

我不断收到一条错误消息,指出“类BankAccount中的构造函数BankAccount无法应用于给定的条件;必填:无自变量; found:int;

我知道与你们正在做的事情相比,这是小奶酪,但是我对此并不陌生。 提前致谢。

您没有提供任何构造函数,因此Java创建了一个没有参数的默认构造函数,它什么也不做。

提供一个采用您要提供的int的单参数构造函数。

您没有BankAccount任何显式构造函数。 您似乎将名为BankAccountstatic方法(不要这样做;它要求弄混)与构造函数混淆。 您可能打算执行以下操作:

public class BankAccount {
    private double balance;

    public BankAccount(double balance) {
        this.balance = balance;
    }
}

然后将构建BankAccount对象的其他代码放在其他位置,例如main (可以将main放在相同的类上,这没关系,但它的去向本质上是无关紧要的。重要的是,您要构造的类需要一个构造函数。)

您的BankAccount类需要一个类似以下的构造函数。

public BankAccount(int funds){
    this.funds = funds;
}

假设BankAccount类具有一个称为funds的属性,该属性可以表示例如可用资金。

public static void BankAccount(double x)

Java中没有静态构造函数。 这就是为什么它不被重新构造为构造函数的原因。 只需删除下面指出的staticvoid

因为没有自动定义的构造函数,所以在编译时会添加没有参数的构造函数,并且因为“您的”构造函数与您要调用的构造函数不匹配,因此会出现此错误。

发生这种情况是因为您尚未定义接受int BankAccount构造函数。 修改您的代码,如下所示:

import java.util.ArrayList;
public class BankAccount
{
    // public constructor of BankAccount class
    public BankAccount(int value){
        // do something here with value
    }
    public static void BankAccount(double x)
    {
        ArrayList<BankAccount> accounts = new ArrayList<BankAccount>();
        BankAccount ted = new BankAccount(400);
        BankAcconut bob = new BankAccount(300);
        BackAccount carol = new BankAccount(500);
        accounts.add(new BankAccount(300));
    }
}
import java.util.ArrayList;


public class BankAccount {

private double balance = 0.0;

public BankAccount() {
    // TODO Auto-generated constructor stub

}

public BankAccount(double d) {
    // TODO Auto-generated constructor stub
    balance = d;
}

public static void main(String[] args) //BankAccount(double x)
{
    ArrayList<BankAccount> accounts = new ArrayList<BankAccount>();
    BankAccount ted = new BankAccount(400.0);
    BankAccount bob = new BankAccount(300.0);
    BankAccount carol = new BankAccount(500);
    accounts.add(new BankAccount(300));
}

}

在数字后添加“ D”; 这将使其翻倍。

所以这

TED =新的BankAccount(400);

TED =新的BankAccount(400D);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM