简体   繁体   English

如何通过他们实现的接口找到实例?

[英]How to find the instance by the interface they implements?

I have 2 java classes extends from 2 interface which are inherit relationship like below:我有 2 个 java 类从 2 个接口扩展,它们的继承关系如下:

interface Base{}
interface Sub extends Base{}

class BaseImpl implements Base{}
class SubImpl implements Sub{}

Then I have one Manager class to provide add and findByClass method.然后我有一个经理 class 来提供 add 和 findByClass 方法。

class Manager{
    List<Object> list = new ArrayList<>();

    public void add(Object o){
        list.add(o);
    }

    public List<Object> findByClass(Class clazz){
        //todo: add find logic
    }
}

And I add instances of BaseImpl and SubImpl to Manager and want to query them by interface class as below: return BaseImpl and SubImpl when query by Base interface class and return SubImpl only when query by Sub interface class.我将 BaseImpl 和 SubImpl 的实例添加到 Manager 并希望通过接口 class 查询它们,如下所示:通过基本接口 class 查询时返回 BaseImpl 和 SubImpl,仅在通过子接口 ZA2F2ED4F8EBDCC2CBB4C21A29 查询时返回 SubImpl

    Manager m = new Manager();
    m.add(new BaseImpl());
    m.add(new SubImpl());

    m.findByClass(Base.class); //return BaseImpl and SubImpl
    m.findByClass(Sub.class); //return SubImpl only!
    

My question is how to implement the findByClass method in Manager?我的问题是如何在 Manager 中实现 findByClass 方法?

new BaseImpl().getClass();

This is a silly way to do BaseImpl.class - that one does not require a no-args constructor, does not require a non-abstract class, and doesn't create a pointless instance.这是执行BaseImpl.class的一种愚蠢方法 - 不需要无参数构造函数,不需要非抽象 class,并且不会创建无意义的实例。

Class<? extends BaseImpl>

Similarly, needlessly complicated.同样,不必要的复杂。

Just write:写吧:

Class<BaseImpl> base = BaseImpl.class;

How to get the inherit relationship from class instance?如何从 class 实例中获取继承关系?

base.getSuperclass() and base.getInterfaces() are the methods you want. base.getSuperclass()base.getInterfaces()是您想要的方法。 You'd have to recursively call it all, repeatedly, until you have created a complete list of all types for both base and sub and then you can check if there is overlap.您必须递归地反复调用它,直到您为basesub创建了所有类型的完整列表,然后您可以检查是否存在重叠。

This is a bizarre thing to want to do, so maybe you want to take a step back and explain why you think you need this.这是一件很奇怪的事情,所以也许你想退后一步,解释为什么你认为你需要这个。 Trivially, all classes always share 'a relationship', as everything extends java.lang.Object .简单地说,所有总是共享“关系”,因为一切都扩展java.lang.Object

If you merely want to know if eg SubImpl.class is a subtype of BaseImpl, there is a method for that:如果您只想知道例如SubImpl.class是否是 BaseImpl 的子类型,则有一种方法:

System.out.println(BaseImpl.class.isAssignableFrom(SubImpl.class));
> true

You can use Class#getSuperClass() to get the superclass of a class:您可以使用Class#getSuperClass()来获取 class 的超类:

System.out.println(SubImpl.class.getSuperClass() == BaseImpl.class); // true
System.out.println(new SubImpl().getClass().getSuperClass() == new BaseImpl().getClass()); // true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM