简体   繁体   English

Java的“键值编码”

[英]“Key-Value Coding” for 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. 在Apple上的Objective-C中,有一种称为“键值编码”的东西,它允许您使用类似于文件系统路径的字符串遍历对象图。 There's an informal protocol (ie interface) that allows objects to return values based on the "key" they're asked for. 有一个非正式的协议(即接口),允许对象根据他们要求的“密钥”返回值。 eg The default is to return the value of a field named by the key, while relational collections like NSDictionaries can implement more interesting behavior. 例如,默认是返回由键命名的字段的值,而像NSDictionaries这样的关系集合可以实现更有趣的行为。

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? Java有这样的东西吗? It would be easy enough to implement, but I thought I'd look first. 这很容易实现,但我想我先看看。

PropertyUtils.getProperty ,从Apache的BeanUtils的图书馆,这是否对JavaBean属性。

Hmm, well, in case anyone else has this question, it looks like MVEL is a good bet: 嗯,好吧,万一其他人有这个问题,看起来MVEL是一个不错的选择:

http://mvel.codehaus.org/Property+Navigation http://mvel.codehaus.org/Property+Navigation

Ujorm是一个开源Java库,提供基于键值架构的对象,请参阅示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM