如何克服原语按值传递的事实

发布于 2024-08-18 22:03:32 字数 207 浏览 8 评论 0原文

我有一段很长的代码,可以为我计算两个值(double),我在几个地方使用了这段代码 - 为了坚持 DRY 原则,我应该将这段代码重构为一个很好的代码可单元测试的方法。但是我不能让它返回两个双精度数,而双精度数是原始的,因此不能按值传递和操作。我能想到的最简洁的方法是让这个方法返回一个double[]。有人能想出更好的方法吗?

谢谢

I have a long piece of code that calculates two values (doubles) for me, I use this piece of code in a few places - to stick with DRY principles I should refactor this bit of code to a nice unit testable method. However I cant make it return two doubles, and doubles are primitive so cannot be passed by value and manipulated. The cleanest way I can think of doing this is by making this method return an double[]. Can anyone think of a better way?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(10

蓝色星空 2024-08-25 22:03:32

首先,在 Java 中,所有变量都是按值传递的,而不仅仅是基元。只是对象可以是可变的。理解这一点很重要。例如:

public void addHour(Date date) {
  date.setTime(date.getTime() + 3600 * 1000);
}

日期是按值传递的,但 Date 是可变的,因此可以修改它,但尝试这样做:

public void addHour(Date date) {
  date = new Date(date.getTime() + 3600 * 1000);
}

它不会更改日期。为什么?因为日期是一个引用,但是是按值传递的。

其次,这些双打是否以某种方式相互关联?如果是这样,请将它们包装在一个类中,然后描述这种关系,如下所示:

public class Coordinate {
  private final double x;
  private final double y;

  public Coordinate(double x, double y) {
    this.x = x;
    this.y = y;
  }

  public double getX() { return x; }
  public double getY() { return y; }
}

Firstly, all variables are passed by value in Java, not just primitives. It's just that objects can be mutable. It's important to understand that. For example:

public void addHour(Date date) {
  date.setTime(date.getTime() + 3600 * 1000);
}

The date is passed by value but Date is mutable so it can be modified but try and do this:

public void addHour(Date date) {
  date = new Date(date.getTime() + 3600 * 1000);
}

and it won't change the date. Why? Because date is a reference but is passed by value.

Secondly, do these doubles relate to each other in some way? If so wrap them in a class than describes this relationship like:

public class Coordinate {
  private final double x;
  private final double y;

  public Coordinate(double x, double y) {
    this.x = x;
    this.y = y;
  }

  public double getX() { return x; }
  public double getY() { return y; }
}
小帐篷 2024-08-25 22:03:32

为此,您可以将它们封装在一个类中。

您还可以为计算它们的方法提供一个 double[] 参数,并将计算值放入其中。这可能相当有效,因为如果性能很重要,调用者代码可以重用此数组进行连续调用。

You could encapsulate them in a class for this purpose.

You could also give a double[] parameter to the method that calculates them and where it will put the calculated values in. This can be rather efficent as the caller code can reuse this array for successive invocations if performance is important.

请叫√我孤独 2024-08-25 22:03:32

具有两个 double 字段的类(不可变)?甚至可能想向类添加一些有趣的方法。

另一种方法是让该方法采用回调对象。

A class (immutable) with two double fields? Might even want to add some interesting methods to the class.

The other way around is to have the method take a callback object.

被你宠の有点坏 2024-08-25 22:03:32
double[] arr = {val1, val2};
return arr

或者使用封装 2 个值的类似 Pair 的类......

double[] arr = {val1, val2};
return arr

or go with a Pair-like class that encapsulates 2 values...

寄风 2024-08-25 22:03:32

如果两个双精度数可以被认为是值的逻辑配对,那么将它们捆绑在一个简单的对象中可能有意义吗?

If the two doubles can be thought of as a logical pairing of values, then it might make sense to bundle them in a simple object?

清欢 2024-08-25 22:03:32

我更喜欢 C++,但创建一个自己的名为 Pair 的对象,它可以容纳 2 个双精度数,并且可以通过引用传递,这对我来说很有意义。

I'm more of a C++ guy, but creating an object of your own called Pair which can hold 2 doubles and can be passed by reference makes sense to me.

风筝在阴天搁浅。 2024-08-25 22:03:32

如果您愿意的话,创建一个具有两个双精度属性的新类,其中包含 getter 和 setter 以及构造函数(以及 equals 和 hashcode...),并使该方法返回该类型的对象。实现此目的的通用方法是 Pair 类。这是一种常见的模式,您应该可以在任何地方找到代码片段(例如在 netbeans 代码库中)。

Create an new class that has two double properties with getters and setters and constructor if you like (and equals and hashcode...) and make the method return that type of object. A generic way to do that would be a Pair class. This is a common pattern and you should find code snippets everywhere (e.g. in the netbeans code base).

脸赞 2024-08-25 22:03:32

您有多种选择:

  1. 返回一个数组
  2. 返回一个 List
  3. 返回包装两个双精度数的类的对象

顺便说一句,Java 不会通过以下方式传递对象:参考。它按值传递指向对象的指针。
http://javadude.com/articles/passbyvalue.htm

You have several options:

  1. Return an array
  2. Return a List<double>
  3. Return an object of a class that wraps your two doubles

And by the way, Java does not pass objects by reference. It passes pointers to objects by value.
http://javadude.com/articles/passbyvalue.htm

素罗衫 2024-08-25 22:03:32

双数组是最明显的答案。您可以通过使用如下包装对象来使其更安全:

  public class MyTwoDoubles {

       public MyTwoDoubles(double one, double two) { ... }

       public double getOne() { ... }

       public double getTwo() { ... }
  }

A double array is the most obvious answer. You can make it a bit safer by having a wrapper object like this:

  public class MyTwoDoubles {

       public MyTwoDoubles(double one, double two) { ... }

       public double getOne() { ... }

       public double getTwo() { ... }
  }
冷血 2024-08-25 22:03:32

您可以使用引用类型的包装类
对于每种值类型,您都可以找到一个包装类。
对于您的情况,可以使用 java.lang.Double,希望这可以解决目的
但作为一个好的设计,我建议您不要更改方法内的对象值。
相反,以这样的方式重构代码:调用它两次并返回两个不同的值,然后将其分配给原始值。
作为一个好的实践,不建议在方法内更改对象值

You can rather use Wrapper classes which are of reference types
For every value type you can find a wrapper class.
for your case java.lang.Double can be used,hope this solves the purpose
But still as a good design i suggest you not to alter the object value inside the method.
Instead refactor the code in such a way you call it twice and return two different values then assign it to the original.
As a good practice its not advisible to alter object value inside a method

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