如何克服原语按值传递的事实
我有一段很长的代码,可以为我计算两个值(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
首先,在 Java 中,所有变量都是按值传递的,而不仅仅是基元。只是对象可以是可变的。理解这一点很重要。例如:
日期是按值传递的,但
Date
是可变的,因此可以修改它,但尝试这样做:它不会更改日期。为什么?因为日期是一个引用,但是是按值传递的。
其次,这些双打是否以某种方式相互关联?如果是这样,请将它们包装在一个类中,然后描述这种关系,如下所示:
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:
The date is passed by value but
Date
is mutable so it can be modified but try and do this: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:
为此,您可以将它们封装在一个类中。
您还可以为计算它们的方法提供一个 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.
具有两个
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.
或者使用封装 2 个值的类似 Pair 的类......
or go with a Pair-like class that encapsulates 2 values...
如果两个双精度数可以被认为是值的逻辑配对,那么将它们捆绑在一个简单的对象中可能有意义吗?
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?
我更喜欢 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.
如果您愿意的话,创建一个具有两个双精度属性的新类,其中包含 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).
您有多种选择:
List
顺便说一句,Java 不会通过以下方式传递对象:参考。它按值传递指向对象的指针。
http://javadude.com/articles/passbyvalue.htm
You have several options:
List<double>
And by the way, Java does not pass objects by reference. It passes pointers to objects by value.
http://javadude.com/articles/passbyvalue.htm
双数组是最明显的答案。您可以通过使用如下包装对象来使其更安全:
A double array is the most obvious answer. You can make it a bit safer by having a wrapper object like this:
您可以使用引用类型的包装类
对于每种值类型,您都可以找到一个包装类。
对于您的情况,可以使用 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