简体   繁体   English

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

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

I'm trying to practice for my test in Java tomorrow, but I can't figure out what is going on. 我想明天尝试用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));
    }
}

I keep getting an error message that says "constructor BankAccount in class BankAccount cannot be applied to given terms; required: no arguments; found:int; 我不断收到一条错误消息,指出“类BankAccount中的构造函数BankAccount无法应用于给定的条件;必填:无自变量; found:int;

I know this is small cheese compared to what you guys are doing, but I'm new to this. 我知道与你们正在做的事情相比,这是小奶酪,但是我对此并不陌生。 Thanks in advance. 提前致谢。

You haven't supplied any constructors, so Java creates a default constructor , with no arguments, that does nothing. 您没有提供任何构造函数,因此Java创建了一个没有参数的默认构造函数,它什么也不做。

Supply a one-argument constructor that takes an int that you are supplying. 提供一个采用您要提供的int的单参数构造函数。

You don't have any explicit constructor for BankAccount . 您没有BankAccount任何显式构造函数。 You appear to be confusing your static method called BankAccount (don't do that; it's asking to get confused) with a constructor. 您似乎将名为BankAccountstatic方法(不要这样做;它要求弄混)与构造函数混淆。 You probably mean to do something like this: 您可能打算执行以下操作:

public class BankAccount {
    private double balance;

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

Then put that other code that's constructing BankAccount objects somewhere else, like main . 然后将构建BankAccount对象的其他代码放在其他位置,例如main (It's okay to put main on this same class, but it's essentially irrelevant where it goes. What matters is that you need a constructor for the class you're trying to construct.) (可以将main放在相同的类上,这没关系,但它的去向本质上是无关紧要的。重要的是,您要构造的类需要一个构造函数。)

Your class BankAccount needs a constructor that looks like this. 您的BankAccount类需要一个类似以下的构造函数。

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

This assumes that class BankAccount has an attribute called funds , which could represent, for example, the available funds. 假设BankAccount类具有一个称为funds的属性,该属性可以表示例如可用资金。

public static void BankAccount(double x)

There are no static constructors in Java. Java中没有静态构造函数。 Thats why it doesn't get regocnized as a constructor. 这就是为什么它不被重新构造为构造函数的原因。 Just delete the static and the void as pointed out below. 只需删除下面指出的staticvoid

Because there is no constructor defined automatically a constructor is added on compile time that has no arguments and because "your" constructor does not match the constructor you want to call you get this error. 因为没有自动定义的构造函数,所以在编译时会添加没有参数的构造函数,并且因为“您的”构造函数与您要调用的构造函数不匹配,因此会出现此错误。

This happens because you have not defined BankAccount constructor that accepts an int . 发生这种情况是因为您尚未定义接受int BankAccount构造函数。 Modify your code as follows: 修改您的代码,如下所示:

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

}

Add "D" after the numbers; 在数字后添加“ D”; This will make make it a double. 这将使其翻倍。

So this 所以这

BankAccount ted = new BankAccount(400); TED =新的BankAccount(400);

becomes

BankAccount ted = new BankAccount(400D); TED =新的BankAccount(400D);

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

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