在Java中将接口类作为参数传递 [英] Passing Interface Class as a Parameter in Java

查看:211
本文介绍了在Java中将接口类作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个界面:

public interface IMech {

}

以及实现它的类

public class Email implements IMech {

}

和a已实施此方法的第三类:

and a third class that has this method implemented:

public void sendNotification( Class< IMech > mechanism ){
}

现在我试图像这样调用这种方法

now I'm trying to call that method like so

foo.sendNotification(Email.class);

但我一直有异常说:

The method sendNotification(Class<IMech>) in the type RemediationOperator is not applicable for the arguments (Class<Email>)

如果它接口那个类,这不应该工作吗?

Shouldn't this work if it interfaces that class?

推荐答案

也许你需要

public void sendNotification( Class<? extends IMech> mechanism ) { 

这篇关于在Java中将接口类作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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