How do I loop through the interface implementing Classes (and call interface methods of the class) that I have fetched through Refections in Java?

huangapple go评论81阅读模式
英文:

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(); -&gt; 基本上类似于 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(&quot;com.company&quot;);
Set&lt;Class&lt;? extends IDispatchIdsReplaceable&gt;&gt; classesImplementingIDispatchIdsReplaceable = reflections.getSubTypesOf(IDispatchIdsReplaceable.class);

for (Class&lt;? extends IDispatchIdsReplaceable&gt; classImplementingInterface : classesImplementingIDispatchIdsReplaceable) {
    //classImplementingInterface.replace(); -&gt; 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 &#39;replace&#39; in &#39;Class&#39;

答案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&lt;? extends IDispatchIdsReplaceable&gt; classImplementingInterface : classesImplementingIDispatchIdsReplaceable) {
    IDispatchIdsReplaceable instance = classImplementingInterface.getConstructor().newInstance(); // Choose the right constructor, here I&#39;m using the default one
    instance.replace();  
}

EDIT:

The default constructor should be implemented explicitly inside your subclasses or you will get NoSuchMethodException

huangapple
  • 本文由 发表于 2020年9月10日 19:01:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63828302.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定