我有兩個類,一個叫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