微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将实现接口的类添加到ArrayList

为了在接口中实现所有方法,我创建了一个实现接口的抽象类,然后让其他类扩展抽象类并仅覆盖所需的方法.

我正在为我的应用程序构建API / Framework.

我想将作为IMyInterface接口实例的类添加到ArrayList:

ArrayList<Class<IMyInterface>> classes = new ArrayList<>();  
classes.add(MyClass.class);

这是MyClass

class MyClass extends AbstractIMyInterface {}

这是AbstractIMyInterface

class AbstractIMyInterface implements IMyInterface {}

到目前为止这似乎不可能.我上面的方法不起作用:

add (java.lang.class<com.app.IMyInterface>)
in ArrayList cannot be applied to
(java.lang.class<com.myapp.plugin.plugin_a.MyClass>)

我该如何工作,即:添加一个将另一个类扩展到ArrayList的类

解决方法

你需要使用通配符吗?扩展IMyInterface.

ArrayList<Class<? extends IMyInterface>> classes = new ArrayList<>();

在ArrayList< Class< IMyInterface>>类,你只能添加Class< IMyInterface>.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐