简体   繁体   English

自定义编号 class 用作输出参数

[英]Custom number class to be used as out parameter

There is no "out" or "ref" parameter in Java (I maybe wrong because I haven't touched Java in 3 yrs.). Java 中没有“out”或“ref”参数(我可能错了,因为我在 3 年内没有接触过 Java。)。 I am thinking create classes, like MyInteger, MyFloat and MyDouble, to be used as out parameter.我正在考虑创建类,如 MyInteger、MyFloat 和 MyDouble,用作输出参数。 is there a way to combine them into ONE generic class?有没有办法将它们组合成一个通用 class?

Sample code for MyInteger class: MyInteger class 的示例代码:

public class MyInteger
{
   private int value = 0;

   public int getValue(){ return value;}
   public void setValue(int newValue) {value = newValue;}

}

EDIT:编辑:

how to use MyInteger class:如何使用 MyInteger class:

public boolean aMethod(int input, MyInteger output)
{
   boolean status = true;
   //calculation here;
   //set status to false if anything wrong;
   //if nothing wrong do this: output.setValue(newValue);
   return status;
}

EDIT 2:编辑2:

What I am asking is I hope I can combine MyInteger, MyFloat, ... into ONE generic class.我要问的是我希望我可以将 MyInteger、MyFloat、... 组合成一个通用的 class。

You can use AtomicInteger, AtomicLong, AtomicReference etc. They do exactly what you are suggesting, and as an added feature they are highly thread-safe您可以使用 AtomicInteger、AtomicLong、AtomicReference 等。它们完全按照您的建议进行操作,并且作为附加功能,它们是高度线程安全的


And in response to this comment:并回应此评论:

but why there is no AtomicFloat and AtomicDouble但是为什么没有 AtomicFloat 和 AtomicDouble

Here's what the java.util.concurrent.atomic package JavaDocs say:这是java.util.concurrent.atomic package JavaDocs所说的:

Additionally, classes are provided only for those types that are commonly useful in intended applications.此外,仅为那些在预期应用程序中通常有用的类型提供类。 For example, there is no atomic class for representing byte .例如,没有用于表示byte的原子 class 。 In those infrequent cases where you would like to do so, you can use an AtomicInteger to hold byte values, and cast appropriately.在您希望这样做的罕见情况下,您可以使用AtomicInteger来保存字节值,并进行适当的转换。 You can also hold floats using Float.floatToIntBits and Float.intBitstoFloat conversions, and doubles using Double.doubleToLongBits and Double.longBitsToDouble conversions.您还可以使用Float.floatToIntBitsFloat.intBitstoFloat转换来保持浮点数,并使用Double.doubleToLongBitsDouble.longBitsToDouble转换来保持双精度。

My own take: That sounds awfully complicated, I'd go with AtomicReference<Double> , AtomicReference<Float> etc. instead我自己的看法:这听起来非常复杂,我会用 go 和AtomicReference<Double>AtomicReference<Float>等代替

Besides the fact that out parameters are somewhat awkward and I would not recommend them, why don't you use Integer and the like?除了out参数有点尴尬,我不推荐它们,你为什么不使用Integer之类的?

Since they all extend Number you could do:由于它们都扩展了Number你可以这样做:

public class MyNumber<T extends Number>
{
 private T value = null;

 ...
}

IMHO, if you are using Objects / Visitor patterns properly, you should never need to return more than one value from a method.恕我直言,如果您正确使用对象/访问者模式,则永远不需要从方法返回多个值。

Return an object which contains more than one value返回包含多个值的 object

public Pair<Integer, Double> method3();

or use a visitor to recieve more than one value.或使用访问者接收多个值。 Useful if you can many out comes/errors.如果您可以解决很多问题/错误,则很有用。

public interface Visitor {
    public void onValues(int i, double d);
    public void onOtherValues(double d, String message);
}

method(Visitor visitor);

or you can update the object method is called on, rather than return the values.或者您可以更新调用 object 方法,而不是返回值。

public void method() {
    this.i = 10;
    this.d = 100.0;
}

eg例如

Worker worker = new Worker();
worker.method();
int i = worker.i;
double d = worker.d;

or you can return an valid value on a condition.或者您可以在条件上返回有效值。

// returns the number of bytes read or -1 on the end of the file.
public int read(byte[] bytes);

// index of the search key, if it is contained in the array within the specified range; otherwise, (-(insertion point) - 1)
public int binarySearch(Object[] array, Object key);

There is a generic holder you can use AtomicReference有一个通用的持有人可以使用AtomicReference

public void method(AtomicReference<Integer> i, AtomicReference<Double> i);

For some types there is a built in type AtomicBoolean, AtomicInteger, AtomicLong对于某些类型,有一个内置类型 AtomicBoolean、AtomicInteger、AtomicLong

public void method(AtomicBoolean flag, AtomicInteger len);
// OR
public boolean method(AtomicInteger len);

You can also use a plain array您还可以使用普通数组

int[] i = { 0 };
double[] d = { 0.0 };
method2(i, d);

public void method2(int[] i, double[] d);

You can have your new classes subclass Number.你可以让你的新类子类编号。 The only reason Integer, Float, etc. are immutable is because that's how they're implemented. Integer、Float 等是不可变的唯一原因是因为它们就是这样实现的。

Alternately, you could create a NumberHolder class that you pass around, that has a mutable Number field.或者,您可以创建一个您传递的 NumberHolder class,它有一个可变的 Number 字段。

class NumberHolder {

  private Number num;

  public void setNumber(Number num) {
    this.num = num;
  }

  public void getNumber() {
    return num'
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM