Java - 我可以乘以引用值吗?

发布于 2025-01-10 19:30:28 字数 698 浏览 0 评论 0原文

如果我有一个如下的类,只是为了存储一个值:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

狼性发作 2025-01-17 19:30:28

您想要完成的任务无法按照您实现 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)

滥情空心 2025-01-17 19:30:28

按照给定的 RefTest 定义,定义另一个类,例如:

public class RefTestMultiple{
   private RefTest source;
   private int multiplier;

   RefTestMultiple(RefTest source, int multiplier) {
      this.source = source;
      this.multiplier = multiplier;
   }    

   public int getValue() {
        return multiplier * source.getValue();
   }

您可以像这样使用它:

RefTest a = new RefTest();
RefTestMultiple b = new RefTestMultiple(a,2);
a.setValue(5);
System.out.println(b.getValue());

Taking your definition of RefTest as given, define another class like:

public class RefTestMultiple{
   private RefTest source;
   private int multiplier;

   RefTestMultiple(RefTest source, int multiplier) {
      this.source = source;
      this.multiplier = multiplier;
   }    

   public int getValue() {
        return multiplier * source.getValue();
   }

You can use it like this:

RefTest a = new RefTest();
RefTestMultiple b = new RefTestMultiple(a,2);
a.setValue(5);
System.out.println(b.getValue());
冷…雨湿花 2025-01-17 19:30:28

我不认为 b = a 正在做你认为它正在做的事情。相反,您想要的是:

b.setValue(a.getValue());

对于 b = 2a,您想要的是:

b.setValue(2 * a.getValue());

事实上,当 a 的值发生变化时,b 的值不会更新。这不是数学,这些是程序表达式。

I don't think b = a is doing what you think it is doing. What you want instead is:

b.setValue(a.getValue());

And for b = 2a what you want is:

b.setValue(2 * a.getValue());

Indeed the value of b will not update when the value of a changes. This is not math, these are procedural expressions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文