public void deposit(double amount)
{
balance += amount;
}
这就是我在另一个班级打电话的原因。我希望能够将100美元存入此帐户。
Account acct1;
acct1 = new Account(500, "Joe", 1112);
要存入此帐户,我需要做什么?我已尝试过不同的变体(下图),但我对于该怎么做感到困惑。
initBal = new deposit(100);
帮助?
答案 0 :(得分:1)
您想要执行的操作的语法是:
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>, ...);
答案 1 :(得分:0)
确保您的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;
}
}
然后,当您创建帐户acct1 = new Account(500, "Joe", 1112);
然后,要增加帐户余额,您必须调用Account
acct1.deposit(amount)