“键值编码”对于Java
在 Apple 的 Objective-C 中,有一种称为“键值编码”的东西,它允许您使用类似于文件系统路径的字符串来遍历对象图。有一个非正式的协议(即接口)允许对象根据它们所要求的“键”返回值。例如默认是返回由键命名的字段的值,而像 NSDictionaries 这样的关系集合可以实现更有趣的行为。
伪代码示例:
foo.bar = new baz(); foo.bar.mymap = new map(); foo.bar.mymap['bom'] = 2; foo.valueForKeyPath("bar.mymap.bom") # 2
Java 有类似的东西吗?这很容易实现,但我想我应该先看看。
In Objective-C on Apple there is something called "Key-Value Coding" that allows you to traverse the object graph using strings similar to filesystem paths. There's an informal protocol (i.e. interface) that allows objects to return values based on the "key" they're asked for. e.g. The default is to return the value of a field named by the key, while relational collections like NSDictionaries can implement more interesting behavior.
Pseudo code example:
foo.bar = new baz(); foo.bar.mymap = new map(); foo.bar.mymap['bom'] = 2; foo.valueForKeyPath("bar.mymap.bom") # 2
Is there anything like this for Java? It would be easy enough to implement, but I thought I'd look first.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自 apache 的 beanutils 库的 PropertyUtils.getProperty 对 JavaBean 属性执行此操作。
PropertyUtils.getProperty, from apache's beanutils library, does this for JavaBean properties.
嗯,好吧,如果其他人有这个问题,看起来 MVEL 是一个不错的选择:
http:// /mvel.codehaus.org/Property+Navigation
Hmm, well, in case anyone else has this question, it looks like MVEL is a good bet:
http://mvel.codehaus.org/Property+Navigation
Ujorm 是一个开源 Java 库,提供基于键值架构的对象,请参阅示例。
The Ujorm is an open source Java library providing objects based on the key‑value architecture, see the examples.