英文:
How do I loop through the interface implementing Classes (and call interface methods of the class) that I have fetched through Refections in Java?
问题
以下是翻译好的部分:
我尝试使用“反射”获取一组实现了接口IDispatchIdsReplaceable
的类。现在我想要做的是循环遍历这些类,在循环内调用接口方法(重写的方法)并执行实现。我应该如何做到这一点?
伪代码:
public interface IDispatchIdsReplaceable {
void replace();
}
public class Class1 implements IDispatchIdsReplaceable {
//常规类方法
@Override
void replace() {
//实现
}
}
public class Class2 implements IDispatchIdsReplaceable {
//常规类方法
@Override
void replace() {
//实现
}
}
在另一些方法中,需要这个函数:
Reflections reflections = new Reflections("com.company");
Set<Class<? extends IDispatchIdsReplaceable>> classesImplementingIDispatchIdsReplaceable = reflections.getSubTypesOf(IDispatchIdsReplaceable.class);
for (Class<? extends IDispatchIdsReplaceable> classImplementingInterface : classesImplementingIDispatchIdsReplaceable) {
//classImplementingInterface.replace(); -> 基本上类似于 Class1.replace() 或 Class2.replace(),与循环一起。
//如何执行上述类似的函数调用,并运行各个类的重写方法
}
在循环中运行classImplementingInterface.replace()
时会出现错误:
无法解析方法'replace',位于'Class'中
英文:
I have tried to fetch a Set of Classes that implements an interface IDispatchIdsReplaceable
using Refections
. Now what I want to do is loop through these classes, call the interface method (overridden methods) within the loop and perform the implementations. How can I do this?
Pseudo Code:
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
}
}
In some other method, this function is needed
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
}
It gives me error when i run classImplementingInterface.replace()
in the loop
Cannot resolve method 'replace' in 'Class'
答案1
得分: 2
缺失的部分是 replace
方法应该从一个实例中调用,因此你首先需要创建该类的一个实例,然后调用 replace
方法:
for (Class<? extends IDispatchIdsReplaceable> classImplementingInterface : classesImplementingIDispatchIdsReplaceable) {
IDispatchIdsReplaceable instance = classImplementingInterface.getConstructor().newInstance(); // 选择正确的构造函数,在这里我使用了默认的
instance.replace();
}
编辑:
默认构造函数应该在你的子类中明确实现,否则你会得到 NoSuchMethodException
。
英文:
What is missing here is that replace
method should be called from an instance, so you need first to create an instance of that class and then call 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();
}
EDIT:
The default constructor should be implemented explicitly inside your subclasses or you will get NoSuchMethodException
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论