我有两个类,一个叫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);
}
}
这里是的BankAccount方法
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;
}
}
'新的存款()'? 'Deposit'是_class_吗?它在哪里以及如何定义? –
这个网站已经有很多次这么说过了 - 你应该*绝对*考虑提取或重读一篇介绍性的Java文本。您的代码中的错误数量非常重要。 – Perception
先搜索相似的问题。看到这些:http://stackoverflow.com/search?q=non-static+method+Deposit%28%29+cannot+be+referenced+from+a+static+context+[java] – RAS