Java 中是否有等效的 .Net TypeConverter
在 .NET 中,当我有一个可以作为多种类型存在的“值”时,我可以轻松地使用 TypeConverter 在这些类型(货币类型、xml 数据与对象表示等)之间进行切换。在Java中,我不确定处理这种情况的首选方法是什么。 Java 中有类似的 TypeConverter 吗?
In .NET when I had a "value" that could exist as multiple types I could easily use a TypeConverter for switching between those types (currency types, xml data vs object representation, ect). In Java, I am not sure what the preferred way to handle this situation is. Is there a TypeConverter equivalent in Java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于来自 .NET 世界的人来说,如果没有开箱即用的工具,将会感到惊讶。这是因为我们有像基元(int、long)等、它们的基元包装器(Integer、Long 等)、在需要时从 int 到 Integer 的自动装箱(这是来自 JDK 1.5)。
所以我们这些可怜的 Java 开发人员手动转换东西(上面 @Boolean 给出的一些例子)
在执行这些操作时,使用 == 运算符也会带来无尽的麻烦。例如:缓存最多 127 的自动装箱整数。
如果您正在编写一个需要太多数据转换的大型应用程序,您可以自己编写一些东西。 Spring 的“TypeConverter”接口可以是一个不错的开始。
使用此链接 http://imagejdocu.tudor.lu/doku.php ?id=howto:java:how_to_convert_data_type_x_into_type_y_in_java 如果您遇到问题
For someone from a .NET world it is going to be a surprise that there isn't one out of the box. This is because we have things like primitives (int, long) etc, their primitive-wrappers (Integer, Long etc), autoboxing from int to Integer when you require (this is from JDK 1.5).
So we the poor Java developers manually convert things (some examples given above by @Boolean)
Also the endless hassles of using == operators when doing these. For ex: Autoboxed integers of upto 127 are cached.
If you are writing a huge app that needs too much data conversion you can write something on your own. Spring's "TypeConverter" interface can be a decent start.
Use this link http://imagejdocu.tudor.lu/doku.php?id=howto:java:how_to_convert_data_type_x_into_type_y_in_java if you have some trouble
好吧,你可以对事物进行类型转换...例如,如果你有一个包含字符串的 List 对象..你会从列表中获取一个字符串,如下所示:
如果你试图将字符串转换为数字..你会做类似的事情对此:
我在将整数转换为字符串时作弊,方法是:
Well you can typecast things... for example if you have a List object that contained strings.. you would grab a String from the list like this:
If you are trying to convert a string to a number.. you would do something similar to this:
I cheat when converting an integer to a string by doing this:
TypeConverter 添加到 .net 的部分目的是允许交互式 UI 设计人员将值显示为字符串,并让用户编辑 UI 设计人员不理解的类型的专有值。
我认为TypeConverter也是用于数据绑定的。
事实上,您可以在自己的软件中使用 TypeConverter 在编译时已知的类型之间进行转换,这是一个副作用,而不是创建它们的主要用例。
由于 Java 从未尝试过支持“RAD”工具,因此它对 TypeConverter 和 PropertyDescriptors 等没有同样的需求。在某种程度上,.net 的设计目的是允许与 VB6 启用相同类型的 UI 开发。
TypeConverter where added to .net partly to allow interactive UI designers to show values as string and let the user edit propriety values of types the UI designer does not understand.
I think TypeConverter is also used by data binding.
The fact you can use TypeConverter in your own software to convert between types you know at compile time is a side effect and not the primary use case they were created for.
As Java has never tried to support “RAD” tools, it has not had the same need for TypeConverter and PropertyDescriptors etc. To some extent .net was designed to allow the same sort of UI development that VB6 enabled.