Java泛型:绑定不匹配:类型不是类型的有界参数的有效替代

时间:2022-05-25 16:09:23

I am facing the following problem:

我面临的问题是:

I have these class & interface definitions

我有这些类和接口定义

public abstract class ViewModelRefreshPostListFragment<T extends IRefreshPostViewCallback, R extends RefreshPostViewModel<T>>
    extends RefreshPostListFragment implements IRefreshPostView {

    private final ViewModelHelper<T, R> mViewModeHelper = //error here
        new ViewModelHelper<>(); 

...
}

public abstract class RefreshPostViewModel<R1 extends IRefreshPostViewCallback> extends AbstractViewModel<IRefreshPostViewCallback> {}

public class ViewModelHelper<T extends IView, R extends AbstractViewModel<T>> {}

public abstract class AbstractViewModel<T extends IView> {}

public interface IRefreshPostViewCallback extends IView {}

Eclipse gives me still this error: Bound mismatch: The type R is not a valid substitute for the bounded parameter <R extends AbstractViewModel<T>> of the type ViewModelHelper<T,R>

Eclipse仍然给了我这个错误:绑定不匹配:类型R不是有界参数 >的ViewModelHelper类型 的有效替代 ,r> 扩展abstractviewmodel

Based on Java inheritance I created these 2 chains:

基于Java继承,我创建了这两个链:

"Chain" from ViewModelRefreshPostListFragment class definition
1) R extends RefreshPostViewModel<T> -> R extends RefreshPostViewModel<R1 extends IRefreshPostViewCallback> -> R extends AbstractViewModel<IRefreshPostViewCallback>
1.1) T extends IRefreshPostViewCallback
1.2) T (from RefreshPostViewModel<T>) is replaced by <R1 extends IRefreshPostViewCallback> Consitent result from 1.1) and 1.2) so the T parameter should be OK.

从ViewModelRefreshPostListFragment类定义1)R扩展RefreshPostViewModel -> R扩展RefreshPostViewModel -> R扩展AbstractViewModel 扩展irefreshpostviewbbview>

"Chain" from ViewModelHelper class definition
2) R extends AbstractViewModel<T>
2.1) T extends IView, IRefreshPostViewCallback extends IView -> T can be replaced by IRefreshPostViewCallback

从ViewModelHelper类定义2)R扩展AbstractViewModel 2.1) T扩展IView, IRefreshPostViewCallback扩展IView -> T可以被IRefreshPostViewCallback取代

If I apply 2.1) on 1.1) && 1.2) we see, parameter T is consistent

如果我对1.1)& 1.2应用2.1)我们看到,参数T是一致的

From 1) follows R extends AbstractViewModel<IRefreshPostViewCallback> from 2) follows R extends AbstractViewModel<T> and from 2.1) follows that T can be replaced by IRefreshPostViewCallback, If I understand the things correctly, this error should not appear, could someone explain me, why is eclipse giving me the error ??

从1)遵循R扩展AbstractViewModel 从2)遵循R扩展AbstractViewModel 从2.1)遵循T可以被IRefreshPostViewCallback替换,如果我理解正确,这个错误就不会出现,有人能解释一下,为什么eclipse会给我这个错误?

Thank you!

谢谢你!

1 个解决方案

#1


4  

The error message comes from the fact that R is not within its bounds.

错误消息来自于R不在其范围内。

Your ViewModelHelper class extends AbstractViewModel<IRefreshPostViewCallback>, no matter what R1 really is.

您的ViewModelHelper类扩展了AbstractViewModel ,不管R1到底是什么。

In the class ViewModelHelper, change the type argument in the extends clause of AbstractViewModel to R1, instead of IRefreshPostViewCallback.

在类ViewModelHelper中,将AbstractViewModel的extend子句中的类型参数更改为R1,而不是IRefreshPostViewCallback。

public abstract class RefreshPostViewModel<R1 extends IRefreshPostViewCallback>
    extends AbstractViewModel<R1>

and this will eliminate the error.

这将消除误差。

This will pass the proper T along in ViewModelHelper. Instead of R being RefreshPostViewModel<IRefreshPostViewCallback>, you will be using RefreshPostViewModel<T>, fulfilling the bounds.

这将在ViewModelHelper中传递适当的T。不再使用RefreshPostViewModel ,而是使用RefreshPostViewModel ,实现边界。

#1


4  

The error message comes from the fact that R is not within its bounds.

错误消息来自于R不在其范围内。

Your ViewModelHelper class extends AbstractViewModel<IRefreshPostViewCallback>, no matter what R1 really is.

您的ViewModelHelper类扩展了AbstractViewModel ,不管R1到底是什么。

In the class ViewModelHelper, change the type argument in the extends clause of AbstractViewModel to R1, instead of IRefreshPostViewCallback.

在类ViewModelHelper中,将AbstractViewModel的extend子句中的类型参数更改为R1,而不是IRefreshPostViewCallback。

public abstract class RefreshPostViewModel<R1 extends IRefreshPostViewCallback>
    extends AbstractViewModel<R1>

and this will eliminate the error.

这将消除误差。

This will pass the proper T along in ViewModelHelper. Instead of R being RefreshPostViewModel<IRefreshPostViewCallback>, you will be using RefreshPostViewModel<T>, fulfilling the bounds.

这将在ViewModelHelper中传递适当的T。不再使用RefreshPostViewModel ,而是使用RefreshPostViewModel ,实现边界。