首页 > 解决方案 > 如何使用在一个类中使用子类和在另一个类中使用超类的参数覆盖方法?

问题描述

我想覆盖子类中的方法,该方法在方法的参数中使用不同的类,但在它的超类中,它的参数中使用了其他类的超类。例如,

public class ManagerImpl<P extends Action> implements Manager {

    public <T extends Domain> T execute(String blah, Session session) {
        //do something
    }

}
public class ContextManagerImpl<P extends Action> extends ManagerImpl {

    @Override
    public <T extends Domain> T execute(String blah, Session session) {
        //do something
    }

}

这看起来很简单,但是,我在 ManagerImpl 中使用的 Session 类是我在 ContextManagerImpl 中使用的 Session 类的超类。当我尝试覆盖时,我收到一条错误消息,提示“方法执行必须覆盖或实现超类型方法”,大概是因为该方法传递了不同的会话。我无法更改任何一个 Session 类名,我需要在 ContextManagerImpl 的 Session 子类中使用一个方法。有任何想法吗?

标签: javainheritance

解决方案


推荐阅读