“键值编码”对于Java [英] "Key-Value Coding" for Java

查看:122
本文介绍了“键值编码”对于Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Apple上的Objective-C中,有一种称为键值编码的东西,它允许您使用类似于文件系统路径的字符串遍历对象图。有一个非正式的协议(即接口),允许对象根据他们要求的密钥返回值。例如默认是返回键所命名的字段的值,而像NSDictionaries这样的关系集合可以实现更有趣的行为。

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.

伪代码示例:


foo.bar = new baz();
foo.bar.mymap = new map();
foo.bar.mymap['bom'] = 2;

foo.valueForKeyPath("bar.mymap.bom") # 2

是否有类似的内容Java的?它实现起来很容易,但我想我先看看。

Is there anything like this for Java? It would be easy enough to implement, but I thought I'd look first.

推荐答案

PropertyUtils.getProperty ,从apache的beanutils库中,为JavaBean属性执行此操作。

PropertyUtils.getProperty, from apache's beanutils library, does this for JavaBean properties.

这篇关于“键值编码”对于Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆