英文:
I need Java Custom Autoboxing
问题
以下是翻译好的内容:
在Java中是否可以自定义自动装箱?
当输入函数的参数时,我需要自动将java.lang.Number
转换为我的类org.expr.NumberExpression
。对于两个参数,我最终编写了四个非常相似的方法:
public class Assemble {
public NumberExpression add(NumberExpression a, NumberExpression b) {
// 做一些操作
}
public NumberExpression add(NumberExpression a, Number b) {
return add(a, new NumberConstant(b));
}
public NumberExpression add(Number a, Number b) {
return add(new NumberConstant(a), new NumberConstant(b));
}
public NumberExpression add(Number a, NumberExpression b) {
return add(new NumberConstant(a), b);
}
}
因此,我可以输入:
```java
assemble.add(5, assemble.add(7, 3));
assemble.add(5, assemble.add(7, 3), 8, 15); // 不起作用
然而,我认为对于10个参数(我想要的),这变得难以管理;我猜我需要编写1024个类似的方法。
我考虑的另一种解决方案是编写一个类似于:
public NumberExpression add(NumberExpression... numbers) {
// 做一些操作
}
但是我能够将Number
和NumberExpression
混合作为参数输入吗?
英文:
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:
public class Assemble {
public NumberExpression add(NumberExpression a, NumberExpression b) {
// do something
}
public NumberExpression add(NumberExpression a, Number b) {
return add(a, new NumberConstant(b));
}
public NumberExpression add(Number a, Number b) {
return add(new NumberConstant(a), new NumberConstant(b));
}
public NumberExpression add(Number a, NumberExpression b) {
return add(new NumberConstant(a), b);
}
}
So I can type:
assemble.add(5, assemble.add(7, 3));
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:
public NumberExpression add(NumberExpression... numbers) {
// do something
}
But would I be able to type Number
and NumberExpression
mixed together as parameters?
答案1
得分: 2
- 正如人们所说,这是不可能完成的。
- 解决你的问题的通常方法是自己封装这些值,然后只有一个方法(看一下 BigDecimal)
<!-- 注释 -->
assemble.add(
new IntegerConstant(5),
assemble.add(new IntegerConstant(7), new IntegerConstant(3))
或者使用工厂:
assemble.add(
NumberExpression.of(5),
assemble.add(NumberExpression.of(7), NumberExpression.of(3))
其中:
class NumberExpression {
public static IntegerConstant of(Integer v) {
//...
}
public static DoubleConstant of(Double v) {
//...
}
}
英文:
- As people said it can't be done
- How your problem usually solved is you wrap the values yourself and have only one method (look at BigDecimal)
<!-- comment -->
assemble.add(
new IntegerConstant(5),
assemble.add(new IntegerConstant(7), new IntegerConstant(3))
or using a factory:
assemble.add(
NumberExpression.of(5),
assemble.add(NumberExpression.of(7), NumberExpression.of(3))
where:
class NumberExpression {
public static IntegerConstant of(Integer v) {
//...
}
public static DoubleConstant of(Double v) {
//...
}
}
答案2
得分: 0
抱歉,我无法满足你的要求。如果你需要中文翻译,请提供文本并我会进行翻译。如果有其他问题或需求,请随意提问。
英文:
No, you can't define custom autoboxing
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论