简体   繁体   English

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

[英]Java generics: Bound mismatch: The type is not a valid substitute for the bounded parameter of the type

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不是有效替代参数<R extends AbstractViewModel<T>>ViewModelHelper<T,R>类型的<R extends AbstractViewModel<T>>

Based on Java inheritance I created these 2 chains: 基于Java继承,我创建了以下2条链:

"Chain" from ViewModelRefreshPostListFragment class definition ViewModelRefreshPostListFragment类定义中的“链”
1) R extends RefreshPostViewModel<T> -> R extends RefreshPostViewModel<R1 extends IRefreshPostViewCallback> -> R extends AbstractViewModel<IRefreshPostViewCallback> 1) R extends RefreshPostViewModel<T> -> R extends RefreshPostViewModel<R1 extends IRefreshPostViewCallback> -> R extends AbstractViewModel<IRefreshPostViewCallback>
1.1) T extends 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. 1.2)将T (来自RefreshPostViewModel<T> )替换为<R1 extends IRefreshPostViewCallback>来自1.1)和1.2的一致结果,因此T参数应为OK。

"Chain" from ViewModelHelper class definition ViewModelHelper类定义中的“链”
2) R extends AbstractViewModel<T> 2) R extends AbstractViewModel<T>
2.1) T extends IView , IRefreshPostViewCallback extends IView -> T can be replaced by IRefreshPostViewCallback 2.1) T extends IViewIRefreshPostViewCallback extends IViewIRefreshPostViewCallback > 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 extends AbstractViewModel<IRefreshPostViewCallback>从2)开始跟随R extends AbstractViewModel<T>和从2.1)开始跟随T可以被IRefreshPostViewCallback替换,如果我正确理解了这些内容,则不应出现此错误,有人可以向我解释,为什么eclipse给我错误?

Thank you! 谢谢!

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. 无论R1到底是什么,您的ViewModelHelper类都可以扩展AbstractViewModel<IRefreshPostViewCallback>

In the class ViewModelHelper , change the type argument in the extends clause of AbstractViewModel to R1 , instead of IRefreshPostViewCallback . 在类ViewModelHelper ,将AbstractViewModelextends子句中的type参数更改为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 . 这将在ViewModelHelper传递正确的T Instead of R being RefreshPostViewModel<IRefreshPostViewCallback> , you will be using RefreshPostViewModel<T> , fulfilling the bounds. 而不是RRefreshPostViewModel<IRefreshPostViewCallback> ,您将使用RefreshPostViewModel<T>实现边界。

暂无
暂无

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

相关问题 Java泛型,绑定不匹配:类型不是bounded参数的有效替代品 - Java generics , bound mismatch: The type is not a valid substitute for the bounded parameter Java泛型绑定不匹配:类型不是边界参数的有效替代品 - java generics Bound mismatch: The type is not a valid substitute for the bounded parameter 绑定不匹配:该类型不是绑定参数的有效替代 - Bound mismatch: The type is not a valid substitute for the bounded parameter Java Generics - 绑定不匹配:类型 Object 不是绑定参数的有效替代品<e extends comparable<e> &gt;</e> - Java Generics - Bound mismatch: The type Object is not a valid substitute for the bounded parameter <E extends Comparable<E>> 边界不匹配:类型A不是边界参数的有效替代品 <T extends Entity> 类型为TestService <T> - Bound mismatch: The type A is not a valid substitute for the bounded parameter <T extends Entity> of the type TestService<T> 有界不匹配:类型不是有界参数的有效替代品<e extends comparable<e> &gt; 类型</e> - Bound mismatch: The type is not a valid substitute for the bounded parameter <E extends Comparable<E>> of the type 绑定不匹配:MyClass1类型不是绑定参数的有效替代品 <T extends Comparator<T> &gt;类型的人 <T> - Bound mismatch: The type MyClass1 is not a valid substitute for the bounded parameter <T extends Comparator<T>> of the type Person<T> Eclipse中有限的不匹配。 “该类型不是有界参数的有效替代” - Bounded mismatch in Eclipse. “The type is not a valid substitute for the bounded parameter” Dropwizard - 绑定不匹配:不是有界参数的有效替代 - Dropwizard - Bound mismatch: not a valid substitute for the bounded parameter 如何创建泛型子类的实例? 获取错误:“绑定不匹配:类型...不是有界参数的有效替代...” - How do I make an instance of generic subclass? Getting error: “Bound mismatch: The type … is not a valid substitute for the bounded parameter …”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM