2012-12-20 261 views
-1

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

} 
+1

'新的存款()'? 'Deposit'是_class_嗎?它在哪裏以及如何定義? –

+2

這個網站已經有很多次這麼說過了 - 你應該*絕對*考慮提取或重讀一篇介紹性的Java文本。您的代碼中的錯誤數量非常重要。 – Perception

+1

先搜索相似的問題。看到這些:http://stackoverflow.com/search?q=non-static+method+Deposit%28%29+cannot+be+referenced+from+a+static+context+[java] – RAS

回答

1

我不明白你爲什麼這樣寫的代碼。

java中的方法名稱應該以小寫字母開頭,如deposit而不是Deposit

BankAccount是一個類,而Deposit是一個非靜態方法。

因此,對於使用Deposit方法,你必須首先這樣創建BankAccount類的對象/實例:

BankAccount b =new BankAccount(); 

,然後使用任何方法使用該對象引用:

b.Deposit(); 
b.Withdraw(); 

你應該這樣寫:

if(choice == 1) 
{ 
    BankAccount b = new BankAccount(); 
    b.Deposit(); 
} 

same你需要做的退出和其他

else if(choice == 2) 
{ 
    BankAccount b = new BankAccount(); 
    b.Withdrawl(); 
    Driver.Driver = new Driver(); 
} 
0

本聲明:

BankAccount.Deposit() = new Deposit(); 

是沒有意義的。首先,Deposit()實例方法BankAccount。將它稱爲BankAccount的特定實例纔有意義。這正是編譯器所抱怨的。

除此之外,Deposit()返回一個int值,這不是可以出現在賦值語句左側的問題。此外,您沒有提到任何類名爲Deposit,所以我不知道new Deposit()應該是什麼。

您的代碼中似乎還存在類似的問題。例如,下面的語句:

Driver.Driver = new Driver(); 

完全是胡說八道—沒有場Driver.Driver。我建議您閱讀教程Understanding Instance and Class Members

0

你的 '意圖' 的工作版本:

import javax.swing.JOptionPane; 
public class Driver 
{ 
    int choice; 
    String number; 
    BankAccount myAccount=null; 

    public Driver() 
    { 
    myAccount=new BankAccount(); 
    } 

    public void drive() 
    { 
    String number = ""; 
     int choice = 0; 
     String name = "?"; 
     do 
     { 
      number = JOptionPane.showInputDialog("1. Deposit 2. Withdraw 3. Balance 4. Change name 5. Exit"); 
      choice = Integer.parseInt(number); 
     if(choice == 1) 
     { 
      myAccount.Deposit(); 
      }else if(choice == 2) 
      { 
      // mAccount.Withdrawl(); 
      // missing method Withdraw1() 
      }else if(choice == 3) 
      { 
     // BankAccount.getBalance = new getBalance(); 
     // missing method getBalance() 
     // JOptionPane.showDialog(balance); 
      }else if(choice == 4) 
      { 
       JOptionPane.showInputDialog(" Please enter a name"); 
     // todo i guess name should be used somewhere in bankaccount... like myAccount.setName(name) 
      }else if(choice ==5) 
      { 
       // JOptionPane.showDialog("Goodbye" + name); 
     // no showDialog method in JOptionPane 
     return; 
      } 
     }while(choice >= 1 && choice <= 5); 
    } 

    public static void main(String pArgs[]) 
    { 
    Driver driver=new Driver(); 
    driver.drive(); 
    } 
}