java - 取决于实际对象类型的动态引用转换
问题描述
我有类层次结构,例如
ChildA extends Parent
ChildB extends Parent
ChildC extends Parent
然后在我的应用程序中,我通过 Parent 引用获取任何这个孩子的方法。
问题是所有这些孩子都有相同的方法,但他们的父母没有
所以,ChildA, ChildB, and ChildC
都有getSomeValue()
吸气剂,但Parent
没有。
现在我需要解析来自任何这个孩子的值,但是 Parent 参考没有为我提供 API,所以我需要将 Parent 转换为特定的孩子类型。
下面是代表我正在尝试做的片段:
private void processChildren(Parent parent) {
ChildA childA = null;
ChildB childB = null;
ChildC childC = null;
if (parent instanceof ChildA) {
childA = parent;
}
if (parent instanceof ChildB) {
childB = parent;
}
if (parent instanceof ChildC) {
childC = parent;
}
String someValue;
if (Objects.nonNull(childA)) {
someValue = childA.getSomeValue();
} // and the same checks and extracts for each of childs and for many methods
}
正如您所看到的,为了仅提取一个值,我需要创建 3 个引用,然后检查它们以强制转换为特定类型,然后检查实际创建的类型以调用该方法。
问题是如何在运行时正确地将引用转换为特定子引用?我想可以使用反射来编写,尽管即使使用反射我也无法解决它。
此外,即使可能 - 可以这样做吗?
仅供参考:我正在开发一个遗留应用程序,所以我不能更改以前编写的代码,所以我不能在父类中添加这个 API。此外,这些类是从外部 jar 提供的。
解决方案
作为一种可能的解决方案,您可以创建一个由特定子类参数化的映射作为键,供应商作为值。每个供应商都负责铸造和处理特定的方法。
Map<Class<? extends Parent>, Supplier> getValueMap = new HashMap<>();
getValueMap.put(ChildA.class, () -> { return ((ChildA) parent).getValue(); });
getValueMap.put(ChildB.class, () -> { return ((ChildB) parent).getValue(); });
getValueMap.put(ChildC.class, () -> { return ((ChildC) parent).getValue(); });
getValueMap.get(parent.getClass()).get();
推荐阅读
- java - Does the ZGC garbage collector support string deduplication?
- umbraco - 更新到最新版本 Umbraco 7 后生成的模型属性类型更改
- android - flutter android emulator 连接服务协议报错:无法连接到http://127.0.0.1:56237/5LMSAYhIdSY=/
- rust - 编译为 Wasm 时,指向堆分配内存的 Rust 指针可以为 0 吗?
- flutter - Flutter streamBuilder initalData 未按预期工作
- laravel - 在 Laravel 模型中访问 Auth 或 Request
- netlogo - 如何有效地创建所有代理都拥有的一个变量的数据
- javascript - 如何使用 xml 或 Javascript 在 SAP-ui5 中创建水平滚动条
- node.js - 在猫鼬中调用 document.save() 函数后,如何取回更新的文档?
- r - 使用 ggplot 和 ggplotly 进行多面绘图时出现在绘图之外的字母