为什么全局变量不会在同一全局变量内用全局函数更新? Android/Java [英] Why Global variable does not update with global function within the same Global variable? Android/Java

查看:168
本文介绍了为什么全局变量不会在同一全局变量内用全局函数更新? Android/Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于android中的应用程序 我有一个全局变量,

For an application in android I have a global variable,

public class ConstantsUrls {
    public static String GET_CART_ALL_ITEM = "store/3/cart/" + CartFunctions.ReturnUserRrQuote() + "/type/customer"
}

并且有,

public class CartFunctions {
     public static String ReturnUserRrQuote() {
        String user_value = "";
        //some function
        return user_value;
    }
}

当我调用一个函数时,将带有参数ConstantsUrls.GET_CART_ALL_ITEM的A设置为

When I call a function, let A with parameter ConstantsUrls.GET_CART_ALL_ITEM as

A(ConstantsUrls.GET_CART_ALL_ITEM);

我得到的正确值是store/3/cart/100/type/customer,但是当我再次调用

I get the correct value as store/3/cart/100/type/customer but when I call again

具有相同参数的函数A我总是获得与store/3/cart/100/type/customer完全相同的值,即使ReturnUserRrQuote()不再调用第二次获取更新的值.

the function A with the same parameter I always get exactly the same value as store/3/cart/100/type/customer even the ReturnUserRrQuote() not call to get updated value for the second time.

当我调用函数

A("store/3/cart/" + CartFunctions.ReturnUserRrQuote() + "/type/customer")

代替

A(ConstantsUrls.GET_CART_ALL_ITEM);  

我总是得到正确的工作(更新后的正确值)

I always get the correct working (updated correct values)

为什么全局变量不能在同一全局变量内用全局函数更新. 这个Java核心的行为是这样还是其他原因?

Why Global variable does not update with global function within the same Global variable. Is this Java core behave like this or any other reason?

推荐答案

在您的代码中,仅创建和初始化常数GET_CART_ALL_ITEM一次,并且它采用了ReturnUserRrQuote()的当前值.之后,它将不会触发该函数,因为该常数已经具有其值并且不需要新的初始化.

In your code, the constant GET_CART_ALL_ITEM is created and initialize only one time, and it takes the current value of ReturnUserRrQuote(). Later It will not trigger the function as the constant already has its value and does not require a new initialization.

如果在代码ReturnUserRrQuote()的开头=>"100".然后创建GET_CART_ALL_ITEM并使用此值初始化,它将为"store/3/cart/100/type/customer"而不是"store/3/cart/" + ReturnUserRrQuote() + "/type/customer".这是初始化时计算表达式"store/3/cart/" + ReturnUserRrQuote() + "/type/customer"的正当原因,并且结果受常量影响(表达式不受常量影响).

If at the beginning of code ReturnUserRrQuote() => "100". Then GET_CART_ALL_ITEM is created and initialize with this value, it will be "store/3/cart/100/type/customer" and not "store/3/cart/" + ReturnUserRrQuote() + "/type/customer". It's due cause at initialization the expression "store/3/cart/" + ReturnUserRrQuote() + "/type/customer" is evaluated and the result is affected to the constant (the expression is not affected to the constant).

因此,当您以后调用此常量时,应使用ReturnUserRrQuote() =>"250". GET_CART_ALL_ITEM仍然是"store/3/cart/100/type/customer".您尚未重新定义它以包含ReturnUserRrQuote()的新值(并且您不希望Java这样做,否则它将不是常量).

So when you call this constant later, with supposed ReturnUserRrQuote() => "250". GET_CART_ALL_ITEM still is "store/3/cart/100/type/customer". You haven't redefine it to contain the new value of ReturnUserRrQuote() (And you don't want java to do so or it won't be a constant).

在您的情况下,您都想要一个常量,因此ReturnUserRrQuote()更改时它通常不会更改是很正常的.或者,您希望它每次都重新评估,而又不希望有一个常数.您可以执行以下操作:

In your case either you want a constant so it's normal that it don't change whenever ReturnUserRrQuote() changes. Or you want it to re-evaluate at every time and you do not want a constant. You could do something like :

public static final const1 = "store/3/cart/";
public static final const2 = "/type/customer";

//whenever you have to obtain your value
String value = const1 + ReturnUserRrQuote() + const2;

修改: 您谈论的是全局变量而不是常量,但问题是相同的.即使具有非全局变量.

You speak about global variable and not constant but the problem is the same. Even with non global variable.

//Static function that return the number of times it has been called
public static returnNumber() {
    final int i=1;
    return i++;
 }

public static void main() {
    int a = returnNumber(); //Initialize a
    for (j=0; j<10; j++) {
        System.out.println(a); //print the current value of a
    }
}

在此示例中,a将在main的开头进行初始化.表达式returnNumber()将被求值,结果将影响到a.这是对returnNumber的第一个调用,然后结果为1.因此a的值为1,而不是returnNumber().在循环中,我打了10次,然后打印出来. 10次​​a将为1,数字1将被印刷10次.每次我呼叫a时,它都不会呼叫returnNumber().

In this example, a will be initialize at the beginning of main. the expression returnNumber() will be evaluated and the result will be affected to a. Here it's the first call to returnNumber then the result is 1. So the value of a is 1 and not returnNumber(). In the loop I call a 10 times and I print it. The 10 times a will value 1 and the number 1 will be printed 10 times. It does not call returnNumber() every time I call a.

这篇关于为什么全局变量不会在同一全局变量内用全局函数更新? Android/Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆