Java - 我可以乘以引用值吗?
如果我有一个如下的类,只是为了存储一个值:
public class RefTest{
private int value;
public void setValue(int value){
this.value = value;
}
public int getValue(){
return value;
}
}
并且希望 RefTest b 始终是 RefTest a 的两倍,我该怎么办?目前我的值相等:
RefTest a = new RefTest();
a.setValue(5);
RefTest b = new RefTest();
b = a;
System.out.println("RefTest a: "+a.getValue());
System.out.println("RefTest b: "+b.getValue());
a.setValue(10);
System.out.println("RefTest a: "+a.getValue());
System.out.println("RefTest b: "+b.getValue());
但我不确定如何将 b 设置为 2a,因为“b=2a”返回错误(如预期),并且 b.setValue( 2*a.getValue()) 当 a.setValue() 更改时不会更新。
If I have a class as follows, just to store a value:
public class RefTest{
private int value;
public void setValue(int value){
this.value = value;
}
public int getValue(){
return value;
}
}
and want RefTest b to always be double RefTest a, how would I go about it? Currently I have the values equal:
RefTest a = new RefTest();
a.setValue(5);
RefTest b = new RefTest();
b = a;
System.out.println("RefTest a: "+a.getValue());
System.out.println("RefTest b: "+b.getValue());
a.setValue(10);
System.out.println("RefTest a: "+a.getValue());
System.out.println("RefTest b: "+b.getValue());
But I'm not sure how to go about setting b to 2a, as "b=2a" returns an error(as expected), and b.setValue(2*a.getValue()) doesn't update when a.setValue() changes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要完成的任务无法按照您实现
RefTest
的方式完成。您希望 a 和 b 表现不同的事实意味着它们不可能真正相同
RefTest< /代码> 对象。
虽然要求有点奇怪,但从引用的角度来看,您需要实际存储对对象的引用(在本例中为 a ),而不是不是引用的 int 值(该值是在调用时计算和设置的)设置值)。您仍然需要做其他工作来实际确保您的 getValue 始终是 a 值的两倍。
您实际上需要定义两个单独的对象,其中“b”的行为将是变量(可以是对另一个对象的引用),如果它有一个 intValue ,则为该值的两倍
如果您是 Java 新手,请研究实例变量中 int 和对象引用之间的差异,以及组合等模式(在这种情况下可以帮助您)
What you're looking to accomplish can't be done the way you've implemented
RefTest
The fact you want a and b to behave differently means they can't really be the same
RefTest
object.While the requirement is a bit odd, from a reference perspective you need to actually store a reference to the object (a in this case) instead of the int value which is not a reference (that value is computed and set at the time you call setValue). You'll still need to do other work to actually ensure your getValue is always two times whatever value a is.
You'll need to actually define two separate objects, where the behaviour of 'b' would be the variable (which can be a reference to another object) if it has an intValue be two times that value
If you're new to Java please look into differences between int and an object reference in instance variables, and also patterns like composition (which could help you in this scenario)
按照给定的 RefTest 定义,定义另一个类,例如:
您可以像这样使用它:
Taking your definition of
RefTest
as given, define another class like:You can use it like this:
我不认为 b = a 正在做你认为它正在做的事情。相反,您想要的是:
对于 b = 2a,您想要的是:
事实上,当 a 的值发生变化时,b 的值不会更新。这不是数学,这些是程序表达式。
I don't think b = a is doing what you think it is doing. What you want instead is:
And for b = 2a what you want is:
Indeed the value of b will not update when the value of a changes. This is not math, these are procedural expressions.