[英]ArrayList of type object in Java - Error which I can't seem to figure out
[英]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;
我知道与你们正在做的事情相比,这是小奶酪,但是我对此并不陌生。 提前致谢。
您没有BankAccount
任何显式构造函数。 您似乎将名为BankAccount
的static
方法(不要这样做;它要求弄混)与构造函数混淆。 您可能打算执行以下操作:
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中没有静态构造函数。 这就是为什么它不被重新构造为构造函数的原因。 只需删除下面指出的static
和void
。
因为没有自动定义的构造函数,所以在编译时会添加没有参数的构造函数,并且因为“您的”构造函数与您要调用的构造函数不匹配,因此会出现此错误。
发生这种情况是因为您尚未定义接受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.