我需要 Java 自定义自动装箱。

huangapple go评论156阅读模式
英文:

I need Java Custom Autoboxing

问题

以下是翻译好的内容:

在Java中是否可以自定义自动装箱?

当输入函数的参数时,我需要自动将java.lang.Number转换为我的类org.expr.NumberExpression。对于两个参数,我最终编写了四个非常相似的方法:

  1. public class Assemble {
  2. public NumberExpression add(NumberExpression a, NumberExpression b) {
  3. // 做一些操作
  4. }
  5. public NumberExpression add(NumberExpression a, Number b) {
  6. return add(a, new NumberConstant(b));
  7. }
  8. public NumberExpression add(Number a, Number b) {
  9. return add(new NumberConstant(a), new NumberConstant(b));
  10. }
  11. public NumberExpression add(Number a, NumberExpression b) {
  12. return add(new NumberConstant(a), b);
  13. }
  14. }
  15. 因此我可以输入
  16. ```java
  17. assemble.add(5, assemble.add(7, 3));
  18. assemble.add(5, assemble.add(7, 3), 8, 15); // 不起作用

然而,我认为对于10个参数(我想要的),这变得难以管理;我猜我需要编写1024个类似的方法。

我考虑的另一种解决方案是编写一个类似于:

  1. public NumberExpression add(NumberExpression... numbers) {
  2. // 做一些操作
  3. }

但是我能够将NumberNumberExpression混合作为参数输入吗?

英文:

Is it possible to define custom autoboxing in Java?

I need to automatically convert java.lang.Number into my class org.expr.NumberExpression when typing parameters of a function. For two parameters, I ended up writing four very similar methods:

  1. public class Assemble {
  2. public NumberExpression add(NumberExpression a, NumberExpression b) {
  3. // do something
  4. }
  5. public NumberExpression add(NumberExpression a, Number b) {
  6. return add(a, new NumberConstant(b));
  7. }
  8. public NumberExpression add(Number a, Number b) {
  9. return add(new NumberConstant(a), new NumberConstant(b));
  10. }
  11. public NumberExpression add(Number a, NumberExpression b) {
  12. return add(new NumberConstant(a), b);
  13. }
  14. }

So I can type:

  1. assemble.add(5, assemble.add(7, 3));
  2. assemble.add(5, assemble.add(7, 3), 8, 15); // does not work

However, I think this becomes unmanageable for 10 parameters (that I wanted to do); I guess I would need to write 1024 similar methods.

The other solution I was thinking about is to write a method like:

  1. public NumberExpression add(NumberExpression... numbers) {
  2. // do something
  3. }

But would I be able to type Number and NumberExpression mixed together as parameters?

答案1

得分: 2

  1. 正如人们所说,这是不可能完成的。
  2. 解决你的问题的通常方法是自己封装这些值,然后只有一个方法(看一下 BigDecimal)

<!-- 注释 -->

  1. assemble.add(
  2. new IntegerConstant(5),
  3. assemble.add(new IntegerConstant(7), new IntegerConstant(3))

或者使用工厂:

  1. assemble.add(
  2. NumberExpression.of(5),
  3. assemble.add(NumberExpression.of(7), NumberExpression.of(3))

其中:

  1. class NumberExpression {
  2. public static IntegerConstant of(Integer v) {
  3. //...
  4. }
  5. public static DoubleConstant of(Double v) {
  6. //...
  7. }
  8. }
英文:
  1. As people said it can't be done
  2. How your problem usually solved is you wrap the values yourself and have only one method (look at BigDecimal)

<!-- comment -->

  1. assemble.add(
  2. new IntegerConstant(5),
  3. assemble.add(new IntegerConstant(7), new IntegerConstant(3))

or using a factory:

  1. assemble.add(
  2. NumberExpression.of(5),
  3. assemble.add(NumberExpression.of(7), NumberExpression.of(3))

where:

  1. class NumberExpression {
  2. public static IntegerConstant of(Integer v) {
  3. //...
  4. }
  5. public static DoubleConstant of(Double v) {
  6. //...
  7. }
  8. }

答案2

得分: 0

抱歉,我无法满足你的要求。如果你需要中文翻译,请提供文本并我会进行翻译。如果有其他问题或需求,请随意提问。

英文:

No, you can't define custom autoboxing

huangapple
  • 本文由 发表于 2020年10月25日 02:10:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64516553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定