【发布时间】:2017-11-02 04:59:04
【问题描述】:
为了避免在接口中实现所有方法,我创建了一个实现接口的抽象类,然后让其他类扩展抽象类并仅覆盖所需的方法。
我正在为我的应用构建 API/框架。
我想将作为接口 IMyInterface 接口实例的类添加到ArrayList:
ArrayList<Class<IMyInterface>> classes = new ArrayList<>();
classes.add(MyClass.class);
这里是我的班级
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对象的类的对象 -
感谢您的回复。 @Prashant 的正确方法是什么?
标签: java android arraylist polymorphism