Userland autoboxing?

。_饼干妹妹 提交于 2019-12-01 03:24:27

问题


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";

And this is what Java would do (as per my own definitions, somewhere, somehow), under the hood:

Foo foo = new Foo();
foo.setLolcat("lolcat");

So, is this possible somehow, or is it a JVM-feature only?


回答1:


No, java does not support operator overloading (http://en.wikipedia.org/wiki/Operator_overloading).

Autoboxing is a compiler feature and not available for your own classes.

The reasoning is explained here: http://www.cafeaulait.org/javafaq.html#xtocid1902938



来源:https://stackoverflow.com/questions/261230/userland-autoboxing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!