繁体   English   中英

java中如何让对象相互交互?

[英]How to make objects interact with each other in java?

我最近开始学习java。 这可能是一个愚蠢的问题,但是否有可能制作一种将余额从“firstAccount”对象转移到“secondAccount”对象的方法?

public class SavingsAccount{
  int balance;

  public SavingsAccount(String name, int balance){
    this.balance = balance;
    this.name = name;
  }

  public static void main(String[] args){
    SavingsAccount firstAccount = new SavingsAccount("A", 5000);
    SavingsAccount secondAccount = new SavingsAccount("B", 3000);
  }
}

我想出的最好的是这个,

public void transfer(int amountToTransfer){
    firstAccount.balance += amountToTransfer;
    secondAccount.balance -= amountToTransfer;
  }

当然它不起作用。 我该如何解决?
提前致谢!

您可以将其设为static方法,要求您传递要使用的两个帐户(您可以随意命名变量),或者创建一个非静态方法,该方法在使用实例时需要一个帐户作为参数。

这是static变化:

public static void transfer(int amountToTransfer, SavingsAccount toAccount, SavingsAccount fromAccount){
    toAccount.balance += amountToTransfer;
    fromAccount.balance -= amountToTransfer;
}

这将在static上下文中使用,例如main ,并将使用YourClass.transfer(yourAmount, firstAccount, secondAccount)

这是将在您的SavingsAccount类中的非静态变体,您可以决定是转移到实例还是从实例转移更有意义:

public void transfer(int amountToTransfer, SavingsAccount toAccount){
    toAccount.balance += amountToTransfer;
    this.balance -= amountToTransfer;
}

这将与您的实例firstAccount.transfer(amount, secondAccount) ,并将金额firstAccount转移到secondAccount 我建议使用这种方式而不是使用static选项。

以下是如何在main使用两者的示例:

public static void main(String[] args){
   SavingsAccount firstAccount = new SavingsAccount("A", 5000);
   SavingsAccount secondAccount = new SavingsAccount("B", 3000);

   int amount = 500;
   firstAccount.transfer(amount, secondAccount); //This is the non-static variation
   transfer(amount, firstAccount, secondAccount); //Static variation, you might need to use the Class.transfer
}

这是很有可能的,但在您的代码中,您的 SavingsAccount 类没有名为“name”的变量。 你应该先创建它。 此外,您应该将两个储蓄账户变量添加到类中并使它们成为静态。 然后,您将能够从该功能访问这些储蓄账户。 您还应该将转账功能设为静态。 您的最终代码可能如下所示:

public static class SavingsAccount {

    int balance;
    String name;

    public static SavingsAccount firstAccount = new SavingsAccount("A", 5000);
    public static SavingsAccount secondAccount = new SavingsAccount("B", 3000);

    public SavingsAccount(String name, int balance) {
        this.name = name;
        this.balance = balance;
    }

    public static void transfer(int amountToTransfer){
        firstAccount.balance += amountToTransfer;
        secondAccount.balance -= amountToTransfer;
    }

    public static void main(String[] args) {
        transfer(put the amount to transfer here);
    }

}

但是是否可以制作一种将余额从“firstAccount”对象转移到“secondAccount”对象的方法?

有很多方法。 除了已经提到的static方法方法。

这里有一些变化。 但最终,想法是相同的 - 您需要 2 个对象实例。

想法1

//Letting the object itself interact with another object (simplified):

public class SavingsAccount{
    String name;
    int balance;

    public SavingsAccount(String name, int balance){
      this.balance = balance;
      this.name = name;
    }

    public void transferTo(SavingsAccount receivingAccount, int amt){
        this.balance -= amt;
        receivingAccount += amt;    //recommended to use setter instead
    }  
}

想法2

//Letting the object itself interact with another object:

public class SavingsAccount{
    String name;
    int balance;

    public SavingsAccount(String name, int balance){
      this.balance = balance;
      this.name = name;
    }

    public int deductBalance(int amt){
        this.balance -= amt;
        return amt;
    } 

    public int addBalance(int amt){
        this.balance += amt;
        return amt;
    }  

    public void transferTo(SavingsAccount receivingAccount, int amt){
        receivingAccount.addBalance(this.deductBalance(amt));
    }
}

class TesterClass{
    SavingsAccount accountA = new SavingsAccount("A", 5000);
    SavingsAccount accountB = new SavingsAccount("B", 3000);
    accountA.transferTo(accountB, 100);   //transfer $100 from A to B
}

想法3

//Letting a 3rd party to let both objects intract. 
//The 2 objects doesn't need to each other's existance.

public class SavingsAccount{
    String name;
    int balance;

    public SavingsAccount(String name, int balance){
      this.balance = balance;
      this.name = name;
    }

    public void deductBalance(int amt){
        this.balance -= amt;
    } 

    public void addBalance(int amt){
        this.balance += amt;
    }    
}

class TesterClass{
    SavingsAccount accountA = new SavingsAccount("A", 5000);
    SavingsAccount accountB = new SavingsAccount("B", 3000);

    public static void transferMoney(SavingsAccount fromAcc, SavingsAccount toAcc, int amt){
        fromAcc.deductBalance(amt);
        toAcc.addBalance(amt);
    }
}

方法的数量还在继续……这里只是一些……

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM