泛型 - 继承 class 修改泛型类型并导致编译器错误
Generics - Inheriting class modifies a Generic Type and causes a compiler error
我有以下摘要class:
public abstract class PresenterWithBreadCrumb<V extends View> extends PresenterWidget<V> {
...
我想做的是扩展这个class并修改类型V。除了View接口提供的方法外,我还需要一个方法。新实例如下。
public abstract class ApplicationPanelPresenter<V extends ApplicationPanelPresenter.ApplicationPanelView>
extends PresenterWithBreadCrumb<ApplicationPanelPresenter.ApplicationPanelView> {
public interface ApplicationPanelView extends View {
void clearPanel();
}
当我尝试重构我的代码,并将最初扩展 PresenterWithBreadCrumb
的 class 更改为 ApplicationPanelPresenter
时,我引入了一个编译错误。
样本之前:
public class RequirementsPanelPresenter extends PresenterWithBreadCrumb<RequirementsPanelPresenter.MyView>
implements RequirementsPanelUiHandlers {
interface MyView extends View, HasUiHandlers<RequirementsPanelUiHandlers> {
}
@Inject
RequirementsPanelPresenter(EventBus eventBus, MyView view) {
super(eventBus, view);
getView().setUiHandlers(this);
}
样本之后:
public class RequirementsPanelPresenter extends ApplicationPanelPresenter<RequirementsPanelPresenter.MyView>
implements RequirementsPanelUiHandlers {
interface MyView extends ApplicationPanelPresenter.ApplicationPanelView, HasUiHandlers<RequirementsPanelUiHandlers> {
}
@Inject
RequirementsPanelPresenter(EventBus eventBus, MyView view) {
super(eventBus, view);
getView().setUiHandlers(this);
}
编译错误在语句getView().setUiHandlers(this);
编译错误为:
The method setUiHandlers(RequirementsPanelPresenter) is undefined for
the type ApplicationPanelPresenter.ApplicationPanelView
为什么编译器将 "this" 解释为 ApplicationPanelPresenter.ApplicationPanelView?我的更改是如何引入此错误的?我该如何解决?
其他上下文
getView()
方法定义在一个父类class和returns一个类型V extends View
.
setUiHandlers方法来自扩展接口HasUiHandlers。方法参数类型为C extends UiHandlers
。 RequirementsPanelPresenter 正在实现的接口 RequirementsPanelUiHandler
扩展了 UiHandlers
.
乍一看,我希望它是
public abstract class ApplicationPanelPresenter<
V extends ApplicationPanelPresenter.ApplicationPanelView>
extends PresenterWithBreadCrumb<V> {
不过,您的代码太复杂了,我无法一眼看出是否可以解决问题。
我有以下摘要class:
public abstract class PresenterWithBreadCrumb<V extends View> extends PresenterWidget<V> {
...
我想做的是扩展这个class并修改类型V。除了View接口提供的方法外,我还需要一个方法。新实例如下。
public abstract class ApplicationPanelPresenter<V extends ApplicationPanelPresenter.ApplicationPanelView>
extends PresenterWithBreadCrumb<ApplicationPanelPresenter.ApplicationPanelView> {
public interface ApplicationPanelView extends View {
void clearPanel();
}
当我尝试重构我的代码,并将最初扩展 PresenterWithBreadCrumb
的 class 更改为 ApplicationPanelPresenter
时,我引入了一个编译错误。
样本之前:
public class RequirementsPanelPresenter extends PresenterWithBreadCrumb<RequirementsPanelPresenter.MyView>
implements RequirementsPanelUiHandlers {
interface MyView extends View, HasUiHandlers<RequirementsPanelUiHandlers> {
}
@Inject
RequirementsPanelPresenter(EventBus eventBus, MyView view) {
super(eventBus, view);
getView().setUiHandlers(this);
}
样本之后:
public class RequirementsPanelPresenter extends ApplicationPanelPresenter<RequirementsPanelPresenter.MyView>
implements RequirementsPanelUiHandlers {
interface MyView extends ApplicationPanelPresenter.ApplicationPanelView, HasUiHandlers<RequirementsPanelUiHandlers> {
}
@Inject
RequirementsPanelPresenter(EventBus eventBus, MyView view) {
super(eventBus, view);
getView().setUiHandlers(this);
}
编译错误在语句getView().setUiHandlers(this);
编译错误为:
The method setUiHandlers(RequirementsPanelPresenter) is undefined for the type ApplicationPanelPresenter.ApplicationPanelView
为什么编译器将 "this" 解释为 ApplicationPanelPresenter.ApplicationPanelView?我的更改是如何引入此错误的?我该如何解决?
其他上下文
getView()
方法定义在一个父类class和returns一个类型V extends View
.setUiHandlers方法来自扩展接口HasUiHandlers。方法参数类型为
C extends UiHandlers
。 RequirementsPanelPresenter 正在实现的接口RequirementsPanelUiHandler
扩展了UiHandlers
.
乍一看,我希望它是
public abstract class ApplicationPanelPresenter<
V extends ApplicationPanelPresenter.ApplicationPanelView>
extends PresenterWithBreadCrumb<V> {
不过,您的代码太复杂了,我无法一眼看出是否可以解决问题。