RxJava Subject无法解析通用使用者

时间:2018-11-22 13:07:25

标签: java generics rx-java2

背景

以下代码来自我的代码的通用总线,该总线将将不同类型的对象(所有对象都从BaseEvent类扩展)委托给不同的使用者。

问题:

   private static PublishSubject<? extends BaseEvent> mPublishSubject;
    @SuppressLint("CheckResult")
    public void subscribeToEventBus(Class subscribingClass, Class eventToSubscribeTo, Consumer<? extends BaseEvent> subscriber) {
        mPublishSubject.subscribe(subscriber);// <---- ERROR HERE
        mEventBusSubscribersList.add(new EventBusInfo(subscribingClass, eventToSubscribeTo, subscriber));
    }

错误:

            error: no suitable method found for subscribe(Consumer<CAP#1>)
method Observable.subscribe(Consumer<? super CAP#2>) is not applicable
(argument mismatch; Consumer<CAP#1> cannot be converted to Consumer<? super CAP#2>)
method Observable.subscribe(Observer<? super CAP#2>) is not applicable
(argument mismatch; Consumer<CAP#1> cannot be converted to Observer<? super CAP#2>)
where CAP#1,CAP#2 are fresh type-variables:
CAP#1 extends BaseEvent from capture of ? extends BaseEvent
CAP#2 extends BaseEvent from capture of ? extends BaseEvent

目标:

我希望Consumer <>接受从BaseEvent扩展的所有类。

考虑到我对Java泛型的原始了解,我不太确定自己到底在做错什么。

0 个答案:

没有答案