java - 如何在其他类中注入父类,以便它根据传递的模型选择子类?
问题描述
我在 Java 中有以下类
class A which is inherited by class A1 and A2
A has method run and both A1, A2 are overriding this method
Another model class B
B has JSON subtypes B1 and B2 (child classes)
我现在想要实现的是如果我跑
InstanceOfA.run(B) where B is a reference to B1 subtype, it should execute A1's run method
and If I run InstanceOfA.run(B) where B is a reference to B2 subtype, It should execute A2's run method.
InstanceOfA 是 A 的一个实例。
如果这是可能的,我可以在所有其他类(需要它的地方)中使用 @Inject 注释注入 A 并且不需要传递实际的实现。
也欢迎任何其他解决方案。
解决方案
推荐阅读
- uproot - 如何使用 uproot 将带有 TVector3 分支的 TTree 加载到 Pandas DataFrame 中
- python - 如何根据嵌套列表中的元素删除列表列表的重复项
- flutter - 如何从父小部件更改一个子小部件的状态,其中子小部件是单独文件中的单独类?
- c# - 自定义验证属性
- regex - 正则表达式仅检查 3 个字符
- java - 如何在 Tomcat 启动时从 xml 读取环境变量?
- python - 在python中创建一个按钮
- python - 动态显示字典的标签连接
- c# - Facebook Graph API - IG Media 获取元数据问题
- sql - 在 postgres 中以文本形式返回当前月份