繁体   English   中英

Java动态更新变量

[英]Java dynamically updating variables

我证实了我最初的想法,即在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中可行吗?

您可以做的就是使用吸气剂。 例如你可能有

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 */

每当getThird()时,它将始终运行代码。 这是使用getter而不是直接访问字段的部分原因。 也就是说,该字段可能不像您想象的那么琐碎,并且您不想更新所有使用它的代码,只需将其更改为一个位置即可。

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

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

暂无
暂无

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

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