gpt4 book ai didi

java - 实例化对象时编译错误

转载 作者:行者123 更新时间:2023-12-02 04:17:44 25 4
gpt4 key购买 nike

我只是想制作一个对象。但我不知道为什么会出现这个错误。我认为一切都正确,但构造函数错误弹出。它说需要 String,String,int 无参数。这是为什么?

error: constructor Account in class Account cannot iven types required: String,String,int found: no arguments reason: actual and formal argument

我的代码

class Account{
private String accName;
private String accID;
private int balance;
private Account(){
accName = "No name found";
accID = "No id found";
}
private Account(String name, String id, int bal){
accName = name;
accID = id;
balance = bal;
}
private void Withdrow(int amount){
if(balance > amount)
{
balance = balance - amount;
System.out.println("Balance :"+balance);
}
else
System.out.println("Not Enough money");
}
private void deposit(int amount){
if(amount>0)
{
balance = balance + amount;
System.out.println("Balance :"+balance);
}
else
System.out.println("Error Deposit");
}
private void showInfo(){
System.out.println("Name :"+ accName);
System.out.println("ID :"+ accID);
System.out.println("Balance :"+ balance);
}
}

class Main{

public static void main(String args[]){

Account ac = new Account();

}
}

最佳答案

公开您的构造函数并初始化 balance 属性

public Account(){
accName = "No name found";
accID = "No id found";
balance = 0;
}

关于java - 实例化对象时编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56653974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com