【问题标题】:Sending a variable to a different class将变量发送到不同的类
【发布时间】:2012-12-08 13:34:49
【问题描述】:

我在发送可变余额和存款以从 Driver 转移到 BankAccount 时遇到问题,有人有什么建议吗?我正在使用 BlueJ。

这里是驱动类和方法-

   import javax.swing.JOptionPane;
public class Driver
{
    int choice;
    String number;
    String name;
    double deposit;
    double withdraw;
    double balance;
    //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)
         {
             String input = JOptionPane.showInputDialog("How much would you like to deposit?");
             deposit = Integer.parseInt(input);
             BankAccount b = new BankAccount();
             b.Deposit();
             Driver a = new Driver();

这里是 BankAccount 程序-

public class BankAccount
{
double balance = 400;
double deposit;
double withdraw;
double Interest = 1.05;
String name;
String accountNumber;

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

【问题讨论】:

  • 您将参数添加到您的存款方式,例如b.存款(存款);或者在银行账户类中创建一个构造函数。
  • 请注意,您不要将变量发送到类;您可以通过定义参数来将它们发送到构造函数或方法以接受来自另一个变量的值。

标签: java class variables bluej


【解决方案1】:

您必须在 BankAccount 类中使用您的 depost 方法,如下所示

public double Deposit(double deposit){

...........

}

你的 Drive 类应该是

...
    b.Deposit(deposit);
...

方法调用

【讨论】:

    猜你喜欢
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多