如何遍历实现 类 的接口(并调用 class 的接口方法),我通过 Java 中的 Refections 获取了该接口?
How do I loop through the interface implementing Classes (and call interface methods of the class) that I have fetched through Refections in Java?
我已尝试使用 Refections
获取 实现 接口 IDispatchIdsReplaceable
的一组 类。现在我要做的是循环遍历这些 类,在循环中调用接口方法(覆盖方法)并执行实现。我该怎么做?
伪代码:
public interface IDispatchIdsReplaceable {
void replace();
}
public class Class1 implements IDispatchIdsReplaceable {
//usual class methods
@Override
void replace() {
//implementation
}
}
public class Class2 implements IDispatchIdsReplaceable {
//usual class methods
@Override
void replace() {
//implementation
}
}
在其他一些方法中,需要这个功能
Reflections reflections = new Reflections("com.company");
Set<Class<? extends IDispatchIdsReplaceable>> classesImplementingIDispatchIdsReplaceable = reflections.getSubTypesOf(IDispatchIdsReplaceable.class);
for (Class<? extends IDispatchIdsReplaceable> classImplementingInterface : classesImplementingIDispatchIdsReplaceable) {
//classImplementingInterface.replace(); -> basically somthing like Class1.replace() or Class2.replace() along with the loop.
//How to do the above sort of function call and run those overriden methods of the respective class
}
当我在 运行 classImplementingInterface.replace()
循环中出现错误时
Cannot resolve method 'replace' in 'Class'
这里缺少的是 replace
方法应该从一个实例调用,所以你需要先创建一个 class 的实例,然后调用 replace
:
for (Class<? extends IDispatchIdsReplaceable> classImplementingInterface : classesImplementingIDispatchIdsReplaceable) {
IDispatchIdsReplaceable instance = classImplementingInterface.getConstructor().newInstance(); // Choose the right constructor, here I'm using the default one
instance.replace();
}
编辑:
默认构造函数应该在你的子classes中显式实现,否则你会得到NoSuchMethodException
我已尝试使用 Refections
获取 实现 接口 IDispatchIdsReplaceable
的一组 类。现在我要做的是循环遍历这些 类,在循环中调用接口方法(覆盖方法)并执行实现。我该怎么做?
伪代码:
public interface IDispatchIdsReplaceable {
void replace();
}
public class Class1 implements IDispatchIdsReplaceable {
//usual class methods
@Override
void replace() {
//implementation
}
}
public class Class2 implements IDispatchIdsReplaceable {
//usual class methods
@Override
void replace() {
//implementation
}
}
在其他一些方法中,需要这个功能
Reflections reflections = new Reflections("com.company");
Set<Class<? extends IDispatchIdsReplaceable>> classesImplementingIDispatchIdsReplaceable = reflections.getSubTypesOf(IDispatchIdsReplaceable.class);
for (Class<? extends IDispatchIdsReplaceable> classImplementingInterface : classesImplementingIDispatchIdsReplaceable) {
//classImplementingInterface.replace(); -> basically somthing like Class1.replace() or Class2.replace() along with the loop.
//How to do the above sort of function call and run those overriden methods of the respective class
}
当我在 运行 classImplementingInterface.replace()
循环中出现错误时
Cannot resolve method 'replace' in 'Class'
这里缺少的是 replace
方法应该从一个实例调用,所以你需要先创建一个 class 的实例,然后调用 replace
:
for (Class<? extends IDispatchIdsReplaceable> classImplementingInterface : classesImplementingIDispatchIdsReplaceable) {
IDispatchIdsReplaceable instance = classImplementingInterface.getConstructor().newInstance(); // Choose the right constructor, here I'm using the default one
instance.replace();
}
编辑:
默认构造函数应该在你的子classes中显式实现,否则你会得到NoSuchMethodException