gpt4 book ai didi

java - 多重继承之类的?

转载 作者:行者123 更新时间:2023-12-01 09:27:41 25 4
gpt4 key购买 nike

我有以下类结构(Blah === ExtBlah):

Base
+--[ ExtBase ] --???-- [ ExtBlah ] --- .. more scripts ...
+--[ Blah ]
+---[ Script1 ]
+---[ Script2 ]
+---[ ....... ]
+---[ ScriptN ]

现在我需要编写更多需要扩展 Blah 的脚本,但是 Blah 必须为新的脚本案例扩展“ExtBase”,而不是“Base”。

我仍然必须保持原来的样子,因为 Scripts1-N 必须依赖于“Base”,而不是“ExtBase”。

此外,我不想创建 Blah 的副本,即 ExtBlah,因为这意味着代码重复,并且支持会更加困难和困惑。

有什么想法吗?

最佳答案

正如 Jean-Baptiste 在他的评论中所说,如果你不想重复 Blah 的代码,请将其封装到 ExtBlah 中并调用 Blah 的方法。记得添加一个通用接口(interface)。事实上,接口(interface)试图解决一些多重继承问题。

Base
+--[ ExtBase ]
+--[ Blah ] implements BlahInterface
+---[ Script1 ]
+---[ Script2 ]
+---[ ....... ]
+---[ ScriptN ]
+--[ ExtBlah: - private Blah instance] implements BlahInterface
+---[ ScriptK ]

关于java - 多重继承之类的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39709979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com