About error using Java generics: “type parameter S is not within its bound”

前端 未结 3 992
傲寒
傲寒 2020-12-20 14:06

I am writing some classes using Generics but I can\'t find a solution for the class SolutionsSubset and so I a getting the error \"type parameter S is not within its bound\"

3条回答
  •  Happy的楠姐
    2020-12-20 14:44

    First of all, here is the full error (which is specific to MathSubset not getting a proper parameter): Bound mismatch: The type S is not a valid substitute for the bounded parameter > of the type QifFixer.MathSubset

    The problem is that MathSubset expects a , but you're giving it a S extends Solution - those types having nothing to do with each other, because a Solution does not inherit or implement Comparable.

    If anything, you could try this:

    public class SolutionsSubset> extends
        MathSubset implements Solutions>;
    

    Unfortunately, this will STILL not work because MathSubset implements Iterable, but so does Solutions.

    An easy fix would be for Solutions to not extend Iterable, but it really sounds to me like you're trying to use a more complex approach than you need to. May be a "has-a" instead of "is-a" design might be more beneficial here?

提交回复
热议问题