动态参考投射取决于实际对象类型 [英] Dynamic reference casting depending on actual object type

查看:48
本文介绍了动态参考投射取决于实际对象类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有类层次结构,例如

I have classes hierarchy such as

ChildA extends Parent
ChildB extends Parent
ChildC extends Parent

然后在我的应用程序中,我通过父代引用获得了这个孩子中的任何一个。

then in my application, I get in a method any of this child by Parent reference.

问题是所有这些孩子都有相同的方法,但他们的父母却没有

The problem is all of these children have the same methods but their parent doesn't

因此, ChildA,ChildB和ChildC 都有 getSomeValue() getter,但 Parent 没有

So, ChildA, ChildB, and ChildC all have getSomeValue() getter but Parent doesn't.

现在,我需要解析任何这个孩子的值,但是Parent引用没有为我提供API,因此我需要将Parent转换为特定孩子

Now I need to parse the value from any of this child, but Parent reference doesn't provide me with API so I need to cast Parent to specific children type.

以下是代表我要执行的操作的代码段:

Below is the snippet representing what I am trying to do:

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个引用,然后检查它们以将其转换为特定类型,然后检查实际创建的类型以进行调用

As you can see in order to extract just one value I need to create 3 references, then check them in order to cast to specific type and then check what the type actually was created in order to call the method.

问题是如何在运行时将引用正确转换为特定的子引用?
我想可以用反射来写,尽管即使使用反射也无法解决。

The question is how to properly cast the reference to the specific child reference in runtime? I guess it is possible to write using reflection, although I was not able to solve it even with reflection.

即使有可能,也是吗?可以这样做吗?

Also, even if it possible - is it ok to do that?

仅供参考:我正在使用旧版应用程序,因此无法更改以前编写的代码,因此无法在Parent类中添加此API 。另外,这些类是从外部jar提供的。

FYI: I am working on a legacy application so I can't change the previously written code so I can't add this API in Parent class. Also, the classes are provided from an external jar.

推荐答案

作为一种可能的解决方案,您可以创建由特定子类参数化的映射作为关键,而供应商作为价值。每个供应商都负责转换和处理特定方法。

As a possible solution you can create a map parametrized by specific child Class as a key, and supplier as a value. Each supplier is responsible for casting and dealing with particular methods.

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();

这篇关于动态参考投射取决于实际对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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