我有一个名为“模块”的课程
public abstract class Module {
protected Map<String, Port> ports;
...
public Map<String, Port> getPorts() {
return ports;
}
}
Run Code Online (Sandbox Code Playgroud)
和一个名为 Design 的类,它是 Module 的子类
public class Design extends Module{
...
//want to do this but doesn't compile
@override
public Map<String, Port> getPorts() {
return (Map<String, TopPort>) ports; //TopPort is a subclass of Port
}
}
Run Code Online (Sandbox Code Playgroud)
基本上我想要做的只是将 TopPorts 添加到 Design 并在从 getPorts() 函数返回时已经转换类型。
我现在正在做的是在 getPorts 函数在 main 中返回后将 Ports 转换为 TopPorts,但这非常糟糕,在我的代码上构建的人将无法立即理解何时返回 TopPort。
我还尝试创建另一个函数而不是覆盖 getPorts()
//want to do this as well …
Run Code Online (Sandbox Code Playgroud)