gpt4 book ai didi

java - GWT。创建原始(不可引用)整数变量

转载 作者:行者123 更新时间:2023-11-29 07:15:21 25 4
gpt4 key购买 nike

在我的课上,我有字段int count。我想根据 count 变量的值创建一个新变量,如下所示:int a = new Integer(count)。但是当我更新计数变量:count++ 时,变量 a 也会更新。那么如何创建非引用 int 变量呢?

最佳答案

你不能用 Java 做到这一点。您最接近的选择是创建一个包含单个 int 的封闭类,并改为引用它:

class MutableInteger {
public int value;
}

然后,稍后:

MutableInteger a = new MutableInteger();
a.value = 5;

MutableInteger b = a;
b.value++;

a.value++;

//since a.value is the same primitive as b.value, they are both 7

但是:这打破了 Java 中一系列普遍接受的最佳实践。您可能会寻找替代方法来解决您的实际问题。

关于java - GWT。创建原始(不可引用)整数变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10055428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com