用Java中的通用参数重写一个方法? [英] Overriding a method with Generic Parameters in Java?
问题描述
我有一个抽象类 Monitor.java ,它被Class EmailMonitor.java 分类。
方法:
public abstract List <?扩展MonitorAccount>在Monitor.java中定义了performMonitor(List <?extends MonitorAccount>账户)
EmailMonitor.java 。
我目前在 EmailMonitor.java 中重写了该方法。如下所示:
@Override
public List< EmailAccount> performMonitor(List< EmailAccount> emailAccounts){
//...无关逻辑
返回emailAccounts;
}
然而,这会产生编译时错误: $ b $名称冲突:类型为EmailMonitor的方法performMonitor(List< EmailAccount>)与Monitor类型的performMonitor(Lis<?extends MonitorAccount> emailAccounts)具有相同的擦除,但不包含覆盖它
EmailAccount
是 MonitorAccount
,所以(至少在我看来)以这种方式覆盖它是非常有意义的。虽然看到编译器对我的逻辑不满意,但我应该如何正确地进行此操作,同时仍然保持编译时间检查以确保对 EmailMonitor.performMonitor()
接收 EmailAccount
列表,而不是某些其他类型的 MonitorAccount
?
不,它不会正确覆盖它。覆盖意味着你应该能够处理任何有效的基类输入。考虑一下如果客户这样做会发生什么:
列表< NonEmailAccount> nonEmailAccounts = ...;
x.performMonitor(nonEmailAccounts);
在你的描述中,没有任何东西会给编译时错误带来麻烦 - 但这显然是错误的。
听起来像 Monitor 应该是它可以监控的账户类型的通用名称,所以您的
EmailMonitor
应该扩展 Monitor< EmailAccount>
。所以:
public abtract class Monitor< T extends MonitorAccount>
{
...
public abstract List <?延伸T> performMonitor(
List< ;? extends T>帐户);
}
公共类EmailMonitor扩展了Monitor< EmailAccount>
{
@Override
public abstract List <?扩展EmailAccount> performMonitor(
List< ;? extends EmailAccount>账户)
{
// Code goes here
}
}
您可能想仔细考虑 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屋!