简体   繁体   English

Java-将值添加到另一个类的变量中

[英]Java - add value to it to a variable from another class

My problem is how do I add a value to a variable from another class, for example: 我的问题是如何为另一个类的变量添加值,例如:

private int balance;

then later in the methods in the same class, I add values to the balance with no problem using: 然后稍后在同一类的方法中,我可以毫无问题地将值添加到余额中:

balance += 50;

but when I go to another class, and type the object then the setter for the balance: 但是当我上另一个课时,键入对象,然后输入余额的设置器:

myObject.setBalance(50);

but the problem strikes here, when I go to the first class and return the balance from there, i get nothing (or the old value of balance) in other words, the NEW value of balance is not being added. 但问题出在这里,当我去上一等舱并从那里退还余额时,我什么也没得到(或余额的旧值),换句话说,没有添加新的余额值。 Any ideas why? 有什么想法吗? I've been stuck at this point for the last couple of hours 最近两个小时,我一直被困在这一点上

Here's my code: 这是我的代码:

public class Zoo 
{
    private int balance;

    public void setBalance(int balance)
    {
        this.balance = balance;
    }

    public int getBalance()
    {
        return this.balance;
    }
}

MY SECOND CLASS:

public class RandomEvents
{
    private Zoo ZooBalance = new Zoo();

    public void callEventSix()
    {
        System.out.println("A local group has raised money to your Zoo");
        System.out.println("Would you like to accept the money? (y) or (n)");
        Scanner readerEventThree = new Scanner(System.in);
        String readerTwo = readerEventThree.next();
        if ( readerTwo.equals("y") ) 
        {
            ZooBalance.setBalance(166);
            System.out.println("You have accepted the gift");
            System.out.println("Your new balance is " + ZooBalance.getBalance());
        } else if ( readerTwo.equals("n") )
        {
            System.out.println("You have refused the gift");
        }
    }
}

In your case 就你而言

Replace this line: 替换此行:

ZooBalance.setBalance(166);

with: 与:

ZooBalance.setBalance(ZooBalance.getBalance() + 166);
Sytem.out.println(ZooBalance.getBalance()); // 166

You need to make a setter for that class. 你需要做一个setter这个类。 Assuming the name of the class is TestClass . 假设该类的名称为TestClass

class TestClass {
    private int balance = 50;
};
public int getBalance() {
    return this.balance;
};
public void setBalance(int newBalance) {
    this.balance = newBalance;
};

And then in another class: 然后在另一个类中:

TestClass test = new TestClass();
test.setBalance(test.getBalance() + 50);
System.out.println(test.getBalance);// 100

I'm pretty sure there's a difference between adding 50 to a value and setting a value to 50 . 我很确定在值上加上50与将值设为50之间会有区别。

myObject.setBalance(50); will not add 50 , it will change the value to 50 . 不会加50 ,它将值更改为50 To add 50, you'd have to do something along the lines of myObject.setBalance(myObject.getBalance() + 50); 要添加50,您必须按照myObject.setBalance(myObject.getBalance() + 50);

generally there are two ways to this. 通常有两种方法。

First. 第一。 Use getter and setter : 使用gettersetter

Zoo zoo = new Zoo();
zoo.setBalance(zoo.getBalance() + 50);

Second. 第二。 Add an adder method: 添加一个adder方法:

public void addToBalance(int addend) {
    balance += addend;
}

Zoo zoo = new Zoo();
zoo.addToBalance(50);

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

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