簡體   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