How to add classes that implement interfaces to ArrayList

In order to implement all methods in the interface, I created an abstract class that implements the interface, and then let other classes extend the abstract class and override only the required methods

I'm building an API / framework for my application

I want to add a class that is an instance of the IMyInterface interface to the ArrayList:

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

This is MyClass

class MyClass extends AbstractIMyInterface {}

This is abstractimyinterface

class AbstractIMyInterface implements IMyInterface {}

So far this seems impossible My above method doesn't work:

How do I work by adding a class that extends another class to ArrayList

Solution

Do you need to use wildcards? Extend IMyInterface

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

In the ArrayList < class < IMyInterface > > class, you can only add class < IMyInterface >

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>