Userland自动装箱? [英] Userland autoboxing?
问题描述
是否可以为自己的类实现自动装箱?
Is it possible to implement autoboxing for your own classes?
为了说明我的例子,我可能想写这个:
To illustrate my example, this is what I might want to write:
Foo foo = "lolcat";
这就是Java会做的事情(根据我自己的定义,在某处,不知何故),在引擎盖:
And this is what Java would do (as per my own definitions, somewhere, somehow), under the hood:
Foo foo = new Foo();
foo.setLolcat("lolcat");
那么,这可能是某种方式,还是仅仅是JVM功能?
So, is this possible somehow, or is it a JVM-feature only?
推荐答案
不,java不支持运算符重载( http://en.wikipedia.org/wiki/Operator_overloading )。
No, java does not support operator overloading (http://en.wikipedia.org/wiki/Operator_overloading).
Autoboxing是一种编译器功能,不适用于您自己的类。
Autoboxing is a compiler feature and not available for your own classes.
这里解释了推理:
http://www.cafeaulait.org/javafaq.html#xtocid1902938
这篇关于Userland自动装箱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!