【发布时间】:2018-01-17 01:47:51
【问题描述】:
有时我面临将一个对象转换为另一个对象的问题,这意味着我有Source 类型的对象source,我必须从中创建Target 类型的对象target,例如(这是在Java,但任何 OO 语言都适合):
public class Source {
private int id;
private String code;
private float value;
private int secondValue;
...
// getters, setters and other methods
}
public class Target {
private int id;
private String code;
private BigDecimal amount;
...
// getters, setters and other methods
}
通常,这意味着将target 的多个成员设置为从source 读取(或派生)的值,例如:
target.setCode(source.getCode());
target.setAmount(BigDecimal.valueOf(source.getSecondValue() / 100.0));
当Source 类型有十多个方法时,这会让人很沮丧,而且很容易漏掉一些东西。
这是在 IDE 中编辑代码时,所以反射不会立即起作用(当然,如果从插件或类似的插件调用它会起作用)。
Eclipse 中是否有任何命令可以列出实例中所有可用的公共方法?当然这也应该考虑继承的方法(所以从Source 声明类型复制粘贴不起作用)?
【问题讨论】: