English 中文(简体)
• 如何克服价格暴涨的事实
原标题:How to overcome the fact that primitives are passed by value

我有一部很长的法典计算了我的两个价值(doubles),在几个地方,我使用这部法典,坚持德国南盟的原则,我应当把这个法典的范畴改成一个可测试的冰箱。 然而,我不得不翻两番,两倍是原始的,不能通过价值和操纵。 我认为,这样做的最佳方法是将这种方法退回到<条码>。 谁会想更好的办法?

增 编

最佳回答

首先,all变量在 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; }
}
问题回答

为此,你可以把他们归入一个班级。

你们还可以对计算方法以及将计算值的计算方法进行双重计算。 这可能是相当重要的,因为如果业绩很重要,呼吁人守则可以重复使用这一阵列。

一组(可互换)和两个<代码>杜布领域? 甚至想给这几类人增加一些有趣的方法。

另一种方式是使这种方法带有背心。

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

或与具有2种价值观的乳制品类混合体......

如果这两个两倍可被视为一种逻辑的价值观配对,那么把两倍打入一个简单的标子是明智的吗?

我再说一杯C++的颜色,但制造了你自己称之为Pair的物体,可以翻两番,可以参考。

如果你喜欢(并且等于和散列......)的话,创造与采集器、设计师和建筑商有两种双重特性的新类别,并使这种方法能够退回这种物体。 这样做的一种通用方式是奶类。 这是一种常见的情况,你应当找到各地的编码信条(例如,在网人代码基中)。

你有几种选择:

  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

双重阵列是最明显的答案。 你们可以通过拥有像样的精彩物体,使它更加安全:

  public class MyTwoDoubles {

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

       public double getOne() { ... }

       public double getTwo() { ... }
  }

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





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签