我想从我的接口(interface)对象中获取实现类名——有什么方法可以做到这一点吗?
我知道我可以使用 instanceof
来检查实现对象,但在我的应用程序中有将近 20 到 30 个类实现相同的接口(interface)来覆盖一个特定的方法。
我想弄清楚它将调用哪个特定方法。
最佳答案
只需使用 object.getClass()
- 它将返回用于实现您的接口(interface)的运行时类:
public class Test {
public interface MyInterface { }
static class AClass implements MyInterface { }
public static void main(String[] args) {
MyInterface object = new AClass();
System.out.println(object.getClass());
}
}
关于java - Java中如何根据接口(interface)对象获取实现类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24992959/