Java动态更新变量

Java动态更新变量,java,Java,我确认了我最初的想法,即在Java中,只有在明确要求更改时,才能更改变量: public class VariableChangeTest { public VariableChangeTest() { } public static void main(String[] args) { int first = 1; int second = 2; int third = first + second;

我确认了我最初的想法,即在Java中,只有在明确要求更改时,才能更改变量:

public class VariableChangeTest {
    public VariableChangeTest() {

    }

    public static void main(String[] args) {
        int first = 1;
        int second = 2;
        int third = first + second;

        System.out.println("first = " + first); /* first = 1 */
        System.out.println("second = " + second); /* second = 2 */
        System.out.println("third = " + third); /* third = 3 */

        second += first;

        System.out.println("********************");

        System.out.println("first = " + first); /* first = 1 */
        System.out.println("second = " + second); /* second = 3 */
        System.out.println("third = " + third); /* third = 4 */

        first += third;

        System.out.println("********************");

        System.out.println("first = " + first); /* first = 5 */
        System.out.println("second = " + second); /* second = 3 */
        System.out.println("third = " + third); /* third = 8 */
    }
}

在本例中,注释反映了我希望发生的事情,而不是实际发生的事情。实际发生的情况是,
third
始终保持原来的
first+second
状态,即3。我想要的是能够将一个变量分配给一个或多个其他变量的当前值,以便在其右侧赋值之一更改时更新。这在Java中可能吗?

您可以使用getter。e、 你本来可以的

class A {
    private int first, second;

    A(int first, int second) {
       this.first = first;
       this.second = second;
    }
    public int getFirst() { return first; }
    public vodi setFirst(int first) { this.first = first; }
    public int getSecond() { return second; }
    public void setSecond(int second) { this.second = second; }
    public void addSecond(int x) { this.second += x; }
    public int getThird() { return first + second; }
    // a possible implementation
    public void setThird(int third) { second = third - first; }
    public void addThird(int x) { this.first += x/2; this.second += x - x/2; }
}

public static void main(String[] args) {
    A a = new A(1, 2);

    System.out.println("first = " + a.getFirst()); /* first = 1 */
    System.out.println("second = " + a.getSecond()); /* second = 2 */
    System.out.println("third = " + a.getThird()); /* third = 3 */

    a.addSecond(a.getFirst());

    System.out.println("first = " + a.getFirst()); /* first = 1 */
    System.out.println("second = " + a.getSecond()); /* second = 3 */
    System.out.println("third = " + a.getThird()); /* third = 4 */

    a.addThird(3);
    System.out.println("first = " + a.getFirst()); /* first = 2 */
    System.out.println("second = " + a.getSecond()); /* second = 5 */
    System.out.println("third = " + a.getThird()); /* third = 7 */

当您获取third()时,它将始终运行代码。这是使用getter而不是直接访问字段的部分原因。i、 e.该字段可能不像您想象的那么琐碎,您不想更新使用它的所有代码,只需在一个地方更改它。

变量用于保存值,而不是执行计算。如果要从其他值动态派生值,请编写一个方法:

private static int calculateResult(int a, int b) {
    return a + b;
}

public static void main(String[] args) {
    int first = 1;
    int second = 2;

    System.out.println("first = " + first); /* first = 1 */
    System.out.println("second = " + second); /* second = 2 */
    System.out.println("result = " + calculateResult(first, second)); /* result = 3 */

    second += first;

    System.out.println("********************");

    System.out.println("first = " + first); /* first = 1 */
    System.out.println("second = " + second); /* second = 3 */
    System.out.println("result = " + calculateResult(first, second)); /* result = 4 */

    // etc.
}

否,因为java是只通过值传递的。您唯一能做的就是创建一个包装器类型类,并使用动态计算结果的方法。但是只使用Primitives是无法做到这一点的。Java的内置包装器(Double、Integer等)有这个功能吗?没有。我的意思是创建自己的计算器类来返回结果。但是您不能使用java中的原语引用,这正是您所寻找的。这个问题仅仅是为了学习,还是您考虑了一个特定的用例?正如@KevinEsche所指出的,动态计算值有一些潜在的方法,但它依赖于创建一些特定的类。你希望达到什么目标?