java 从不同的类调用一个方法到另一个类和方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13965441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:35:23  来源:igfitidea点击:

Calling a method from a different class to another class & method

javaclassmethodscompiler-errorsbluej

提问by user1917656

I have two classes, one called Driver and another called BankAccount. In Driver there is a method called Driver, and in BankAccount a method called Deposit. I'm getting an error that says, "non-static method Deposit() cannot be referenced from a static context" when I try to call BankAccount.Deposit from my Driver method.

我有两个类,一个叫 Driver,另一个叫 BankAccount。在 Driver 中有一个名为 Driver 的方法,在 BankAccount 中有一个名为 Deposit 的方法。当我尝试从我的 Driver 方法调用 BankAccount.Deposit 时,我收到一条错误消息,指出“无法从静态上下文中引用非静态方法 Deposit()”。

Any advice on what I should do to these lines of code to make it run.

关于我应该对这些代码行做什么以使其运行的任何建议。

 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);
}
}

here is the BankAccount Method

这是 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;
}

}

回答by Abubakkar

I don't understand why you have written your code like this.

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

Method name in java should start with a small letter like depositand not Deposit.

java中的方法名应该以小写字母开头,比如deposit而不是Deposit

BankAccountis a class and Depositis a non-static method in it.

BankAccount是一个类,并且Deposit是其中的非静态方法。

So for using Depositmethod you must first create an object/instance of your BankAccountclass like this :

因此,对于使用Deposit方法,您必须首先BankAccount像这样创建类的对象/实例:

BankAccount b =new BankAccount();

And then use any method using that object reference :

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

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

You should write it like this :

你应该这样写:

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

Same you need to do for withdraw and other

您需要为提款和其他操作做同样的事情

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

回答by Ted Hopp

This statement:

这个说法:

BankAccount.Deposit() = new Deposit();

makes no sense. First, Deposit()is an instancemethod of BankAccount. It only makes sense to call it for a particular instance of BankAccount. That's what the compiler is complaining about.

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

Beyond that, there's the problem that Deposit()returns an intvalue, which is not something that can appear on the left side of an assignment statement. Also, you don't mention any class named Deposit, so I don't know what new Deposit()is supposed to be.

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

You seem to have similar problems further on in your code. For instance, the next statement:

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

Driver.Driver = new Driver();

is utter nonsense—there is no field Driver.Driver.

完全是胡说八道——没有领域Driver.Driver

I recommend that you read the tutorial Understanding Instance and Class Members.

我建议您阅读了解实例和类成员教程。

回答by philippe lhardy

a working version of your 'intention' :

您的“意图”的工作版本:

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();
    }
}