Java继承-尝试用泛型方法从Guava EventBus库中获取总线



根据这个答案,我试图创建一个库,从Guava EventBus库创建一个EventBusAsyncEventBus

所以我尝试创建一个类来扩展Guava的AsyncEventBusClass,所以我创建了RetroAsyncEventBus如下:

public class RetroAsyncEventBus extends AsyncEventBus{
    private AsyncEventBus asyncEventBus;
    public RetroAsyncEventBus(Executor executor) {
        super(executor);
    }
    @Override
    public void register(Object object) {
        super.register(object);
    }
    @Override
    public void unregister(Object object) {
        super.unregister(object);
    }
}

首先在这里我不明白为什么我要Override registerunregister,从我从继承中理解,我应该能够通过RetroAsyncEventBus对象访问这个超级方法,我的意思是我应该能够做到这一点:

RetroAsyncEventBus retroAsyncEventBus = new RetroAsyncEventBus(executor);
retroAsyncEventBus.register(this);

,但我的IDE (netbeans)说我不能与编译错误:cannot find symbolretroAsyncEventBus.register(this);。所以我不得不给他们加Override,我可以忍受。

然后我有我的RetroBus类,在那里我创建和初始化我想要的总线,在singleton方法。这样的:

public class RetroBus {
    private final EventBus bus;
    private final AsyncEventBus asyncEventBusMainThread;
    private final AsyncEventBus asyncEventBus;
    private static RetroBus instance;
    private RetroBus(){
        bus = new EventBus();
        asyncEventBusMainThread = new AsyncEventBus(new EventQueueExecutor());
        asyncEventBus = new AsyncEventBus(Executors.newCachedThreadPool());
    }
    public synchronized static EventBus getDefaultBus()
    {
        if (instance == null) 
        {
            instance = new RetroBus();
            return instance.getDefaultEventBus();
        }
        return instance.getDefaultEventBus();
    }
    public synchronized static AsyncEventBus getAsyncBus()
    {
        if(instance == null)
        {
            instance = new RetroBus();
            return instance.getAsyncEventBus();
        }
        return instance.getAsyncEventBus();
    }
    public synchronized static AsyncEventBus getAsyncBusToMainThread()
    {
        if(instance == null)
        {
            instance = new RetroBus();
            return instance.getAsyncEventBusMainThread();
        }
        return instance.getAsyncEventBusMainThread();
    }
    private EventBus getDefaultEventBus() {
        return this.bus;
    }
    private AsyncEventBus getAsyncEventBus() {
        return this.asyncEventBus;
    }
    private AsyncEventBus getAsyncEventBusMainThread() {
        return this.asyncEventBusMainThread;
    }
}

现在我的库编译得很好。在我的测试项目中,我导入了我的库,然后我尝试这样做:

public class TestClass {
    private RetroAsyncEventBus retroAsyncEventBus;
        public TestClass() {
            retroAsyncEventBus =  (RetroAsyncEventBus) RetroBus.getAsyncBusToMainThread();
            retroAsyncEventBus.register(this);
        }
}

这就是我真正的问题所在。首先,它强迫我使用RetroBus.getAsyncBusToMainThread();,我不明白为什么当RetroAsyncEventBus extends AsyncEventBus时我被迫使用它,所以它们应该是相同的类型,我对吗?

然后,如果我像我一样强制转换它,我得到这个编译错误:the type of getAsyncBusToMainThread(); is erroneous.

谁能解释一下我在这里做错了什么?

您必须强制转换的原因是因为您的方法返回AsyncEventBus而不是RetroAsyncEventBus。这意味着程序必须处理该方法返回的任何可能的AsyncEventBus对象。

更改返回类型,您将不再需要强制转换。

最新更新