将一个方法从一个不同的类调用到另一个类&方法

时间:2012-12-20 04:41:59

标签: java class methods compiler-errors bluej

我有两个类,一个叫做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;
}

}

3 个答案:

答案 0 :(得分:1)

我不明白你为什么写这样的代码。

java中的方法名称应该以{{1​​}}之类的小写字母开头而不是deposit

Deposit是一个类,BankAccount是一个非静态方法。

因此,对于使用Deposit方法,您必须首先创建Deposit类的对象/实例,如下所示:

BankAccount

然后使用该对象引用的任何方法:

BankAccount b =new BankAccount();

你应该这样写:

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

你需要为撤回和其他

做同样的事情
if( choice == 1)
{
     BankAccount b = new BankAccount();
     b.Deposit();
}

答案 1 :(得分:0)

本声明:

BankAccount.Deposit() = new Deposit();

毫无意义。首先,Deposit()BankAccount实例方法。只为BankAccount的特定实例调用它才有意义。这就是编译器所抱怨的。

除此之外,还存在Deposit()返回int值的问题,这不是可以出现在赋值语句左侧的值。另外,你没有提到任何名为Deposit的类,所以我不知道new Deposit()应该是什么。

您的代码中似乎还有类似的问题。例如,下一个声明:

Driver.Driver = new Driver();

完全是胡说八道 - 没有字段Driver.Driver

我建议您阅读教程Understanding Instance and Class Members

答案 2 :(得分: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();
    }
}