在 Java 中使用通用参数覆盖方法? [英] Overriding a method with Generic Parameters in Java?

查看:36
本文介绍了在 Java 中使用通用参数覆盖方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象类 Monitor.java,它是一个类 EmailMonitor.java 的子类.

方法:

公共摘要列表performMonitor(List accounts)

Monitor.java 中定义,必须在 EmailMonitor.java 中覆盖.

我目前在 EmailMonitor.java 中重写了该方法,如下所示:

@Override公共列表performMonitor(List emailAccounts) {//...不相关的逻辑返回电子邮件帐户;}

然而,这会产生编译时错误:

名称冲突:EmailMonitor 类型的 performMonitor(List) 方法与 Monitor 类型的 performMonitor(Lis emailAccounts) 具有相同的擦除功能,但不会覆盖它

EmailAccountMonitorAccount 的子类,所以(至少在我看来)以这种方式覆盖它是非常有意义的.看到编译器对我的逻辑不满意,我应该如何正确地解决这个问题,同时仍然保持我的编译时检查以确保对 EmailMonitor.performMonitor() 的所有调用都收到 EmailAccount 而不是其他类型的 MonitorAccount?

解决方案

不,它没有正确覆盖它.覆盖意味着您应该能够处理对基类的任何有效输入.考虑一下如果客户这样做会发生什么:

Monitor x = new EmailMonitor();ListnonEmailAccounts = ...;x.performMonitor(nonEmailAccounts);

根据您的描述,那里没有任何内容应该会导致编译时错误 - 但它显然是错误的.

在我看来,Monitor 在它可以监控的帐户类型中应该是通用的,所以你的 EmailMonitor 应该扩展 Monitor.所以:

public abtract class Monitor{...公共摘要列表执行监视器(列表帐户);}公共类 EmailMonitor 扩展了 Monitor{@覆盖公共摘要列表执行监视器(列表帐户){//代码在这里}}

您可能需要仔细考虑 performMonitor 调用中的泛型 - 返回值意味着什么?

I have an abstract Class Monitor.java which is subclassed by a Class EmailMonitor.java.

The method:

public abstract List<? extends MonitorAccount> performMonitor(List<? extends MonitorAccount> accounts)

is defined in Monitor.java and must be overridden in EmailMonitor.java.

I currently have the method overridden in EmailMonitor.java as follows:

@Override
public List<EmailAccount> performMonitor(List<EmailAccount> emailAccounts) {
    //...unrelated logic
    return emailAccounts;
}

However, this produces the compile time error:

Name clash: The method performMonitor(List<EmailAccount>) of type EmailMonitor has the same erasure as performMonitor(Lis<? extends MonitorAccount> emailAccounts) of type Monitor but does not override it

EmailAccount is a subclass of MonitorAccount, so (in my mind at least) overriding it in this way makes perfect sense. Seeing as the compiler is not happy with my logic though, How should I go about this correctly while still keeping my compile time checks to make sure that all calls to EmailMonitor.performMonitor() receive Lists of EmailAccount rather than some other type of MonitorAccount?

解决方案

No, it's not overriding it properly. Overriding means you should be able to cope with any valid input to the base class. Consider what would happen if a client did this:

Monitor x = new EmailMonitor();
List<NonEmailAccount> nonEmailAccounts = ...;
x.performMonitor(nonEmailAccounts);

There's nothing in there which should give a compile-time error given your description - but it's clearly wrong.

It sounds to me like Monitor should be generic in the type of account it can monitor, so your EmailMonitor should extend Monitor<EmailAccount>. So:

public abtract class Monitor<T extends MonitorAccount>
{
    ...
    public abstract List<? extends T> performMonitor(
        List<? extends T> accounts);
}

public class EmailMonitor extends Monitor<EmailAccount>
{
    @Override
    public abstract List<? extends EmailAccount> performMonitor(
        List<? extends EmailAccount> accounts)
    {
        // Code goes here
    }
}

You might want to think carefully about the generics in the performMonitor call though - what's the return value meant to signify?

这篇关于在 Java 中使用通用参数覆盖方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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