从另一个类调用另一个类和方法
问题描述:
我有两个类,一个叫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;
}
}
答
我不明白你为什么这样写的代码。
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();
}
答
本声明:
BankAccount.Deposit() = new Deposit();
是没有意义的。首先,Deposit()
是实例方法BankAccount
。将它称为BankAccount
的特定实例才有意义。这正是编译器所抱怨的。
除此之外,Deposit()
返回一个int
值,这不是可以出现在赋值语句左侧的问题。此外,您没有提到任何类名为Deposit
,所以我不知道new Deposit()
应该是什么。
您的代码中似乎还存在类似的问题。例如,下面的语句:
Driver.Driver = new Driver();
完全是胡说八道—没有场Driver.Driver
。我建议您阅读教程Understanding Instance and Class Members。
答
你的 '意图' 的工作版本:
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();
}
}
'新的存款()'? 'Deposit'是_class_吗?它在哪里以及如何定义? –
这个网站已经有很多次这么说过了 - 你应该*绝对*考虑提取或重读一篇介绍性的Java文本。您的代码中的错误数量非常重要。 – Perception
先搜索相似的问题。看到这些:http://*.com/search?q=non-static+method+Deposit%28%29+cannot+be+referenced+from+a+static+context+[java] – RAS