public void deposit(double amount)
{
balance += amount;
}
这就是我在另一堂课上所说的。我希望能够将 100 美元存入该帐户。
Account acct1;
acct1 = new Account(500, "Joe", 1112);
我需要做什么才能存入这个账户?我已经尝试过不同的变体(如下),但我不知道该怎么做。
initBal = new deposit(100);
帮助?
您想要执行的操作的语法是:
Account acct1; //Creating a reference of type Account
acct1 = new Account(500, "Joe", 1112); //Instantiating a new Account object,
//giving a reference to that object to acct1
acct1.deposit(100); //Calling the deposit method in class Account
//On the object referred to by acct1
更一般地,调用对象(具有该方法的类型)上的方法:
<object_reference>.<method_name>(<parameter 1>, <parameter 2>, ...);
确保您的Account
对象存储您的初始余额并且您的deposit
方法会增加它:
例子:
public class Account{
private Double balance;
public Account(Double initBalance, String name, int number){
this.balance = initBalance;
}
public void deposit(double amount)
{
balance += amount;
}
}
然后当你创建一个 Account 实例时acct1 = new Account(500, "Joe", 1112);
然后,要增加您的帐户余额,您必须调用您的实例中的 deposit 方法Account
acct1.deposit(amount)