我有两个类,一个叫 Driver,另一个叫 BankAccount。在 Driver 中有一个称为 Driver 的方法,在 BankAccount 中有一个称为 Deposit 的方法。当我尝试从 Driver 方法调用 BankAccount.Deposit 时,我收到一条错误消息,提示“无法从静态上下文引用非静态方法 Deposit()”。
关于我应该对这些代码行做什么以使其运行的任何建议。
import javax.swing.JOptionPane;
public class Driver
{
int choice;
String number;
//public Driver()
public Driver()
{
String number = JOptionPane.showInputDialog("1. Deposit 2. Withdraw 3. Balance 4. Change name 5. Exit");
int choice = Integer.parseInt(number);
do
{
if( choice == 1)
{
BankAccount.Deposit() = new Deposit();
Driver.Driver = new Driver();
}else if(choice == 2)
{
BankAccount.Withdrawl = new Withdrawl();
Driver.Driver = new Driver();
}else if(choice == 3)
{
BankAccount.getBalance = new getBalance();
JOptionPane.showDialog(balance);
Driver.Driver = new Driver();
}else if(choice == 4)
{
name = JOptionPane.showInputDialog(" Please enter a name");
Driver.Driver = new Driver();
}else if(choice ==5)
{
JOptionPane.showDialog("Goodbye" + name);
}
}while( choice >= 1 && choice <= 5);
}
}
这是银行帐户方法
import javax.swing.JOptionPane;
public class BankAccount
{
double balance = 400;
double deposit;
double withdraw;
double Interest = 1.05;
String name;
String accountNumber;
public BankAccount()
{
name = null;
accountNumber = null;
balance = 0;
}
public double Deposit()
{
String input = JOptionPane.showInputDialog("How much would you like to deposit?");
deposit = Integer.parseInt(input);
if (deposit < 10000)
{
balance = (deposit + balance);
}
return balance;
}
}