简体   繁体   English

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

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

I have recently started learning java.我最近开始学习java。 This might be a silly question, but is it possible to make a method that transfer balance from 'firstAccount' object to 'secondAccount' object?这可能是一个愚蠢的问题,但是否有可能制作一种将余额从“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);
  }
}

The best I came up with is this,我想出的最好的是这个,

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

and of course it doesn't work.当然它不起作用。 How can I fix it?我该如何解决?
Thanks in advance!提前致谢!

You can either make it a static method that requires you to pass the two accounts to be used (you can name the variables whatever you want) or make a non-static method that requires one account as a parameter while using the instance.您可以将其设为static方法,要求您传递要使用的两个帐户(您可以随意命名变量),或者创建一个非静态方法,该方法在使用实例时需要一个帐户作为参数。

Here is the static variation:这是static变化:

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

This would be used in a static context such as a main , and would be called using YourClass.transfer(yourAmount, firstAccount, secondAccount) .这将在static上下文中使用,例如main ,并将使用YourClass.transfer(yourAmount, firstAccount, secondAccount)

Here is the non-static variation that would be inside your SavingsAccount class, you can decide whether it makes more sense to transfer to the instance, or from the instance:这是将在您的SavingsAccount类中的非静态变体,您可以决定是转移到实例还是从实例转移更有意义:

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

This would be used with your instance firstAccount.transfer(amount, secondAccount) , and would be transferring the amount from firstAccount to secondAccount .这将与您的实例firstAccount.transfer(amount, secondAccount) ,并将金额firstAccount转移到secondAccount I recommend using this way rather using the static option.我建议使用这种方式而不是使用static选项。

Below is an example on how to use both from within your main :以下是如何在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
}

It is very possible but in your code, your SavingsAccount class doesn't have a variable called 'name'.这是很有可能的,但在您的代码中,您的 SavingsAccount 类没有名为“name”的变量。 You should create that first.你应该先创建它。 Also, you should add the two savings account variables to the class and make them static.此外,您应该将两个储蓄账户变量添加到类中并使它们成为静态。 Then, you will be able to access those savings accounts from the function.然后,您将能够从该功能访问这些储蓄账户。 You should also make the function to transfer money static.您还应该将转账功能设为静态。 Your final code could look like this:您的最终代码可能如下所示:

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

}

but is it possible to make a method that transfer balance from 'firstAccount' object to 'secondAccount' object?但是是否可以制作一种将余额从“firstAccount”对象转移到“secondAccount”对象的方法?

There are many ways.有很多方法。 Apart from the static method approach already mentioned.除了已经提到的static方法方法。

Here are some variations.这里有一些变化。 But ultimately, the idea is the same - you need the 2 object instances.但最终,想法是相同的 - 您需要 2 个对象实例。

Idea 1 :想法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
    }  
}

Idea 2 :想法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
}

Idea 3 :想法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);
    }
}

The number of ways goes on.. here are just a few..方法的数量还在继续……这里只是一些……

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

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