关于使用Java泛型的错误:“类型参数S不在其绑定范围内” [英] About error using Java generics: "type parameter S is not within its bound"

查看:632
本文介绍了关于使用Java泛型的错误:“类型参数S不在其绑定范围内”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些使用泛型的类,但是我找不到SolutionsSubset类的解决方案
,所以我得到错误
类型参数S不在其范围内。我已经阅读了以前的
关于同样的错误的问题,但我无法解决它的情况。
任何人都可以帮助我提高对泛型的了解吗?任何
引用一本好书(我可以在google中找到许多信息
,但是如果有人可以推荐一本书,教程等将会受到欢迎)。
尽管我试图记住提出问题的规则,但是如果我的问题不符合这些规则,我
道歉。



I有以下类和接口:

 


public interface子集< T extends Comparable< ; T>>扩展了Comparable< Subset< T>>
public class MathSubset< T extends Comparable< T>>扩展TreeSet< T>实现子集< T>

public interface Solution< T扩展Comparable< T>>

public interface Solutions< S extends Solution<?>>扩展了Iterable< S>
public class SolutionsSubset< S extends Solution<>>扩展MathSubset< S>实施解决方案< S>


我需要Subset扩展Comparable。在SolutionsSubset中,类MathSubset存储Solution对象。



预先感谢您

解决方案我怎样才能改变这些定义?为了在 MathSubset 中使用类型参数, SolutionsSubset s S 必须扩展Comparable< S> 。作为一个可编译的例子:

  import java.util.TreeSet; 

interface Subset< T extends Comparable< T>>
扩展了Comparable< Subset< T>> {}

class MathSubset< T扩展Comparable< T>>
扩展TreeSet< T>
实现子集< T>
{
public int compareTo(Subset< T> other){throw new Error(); }
}

接口解决方案< T扩展Comparable< T>> {}

interface Solutions< S extends Solution<>>扩展了Iterable< S> {}

类SolutionsSubset< S扩展解决方案<?> &安培;可比< S>>
扩展了MathSubset< S>
实施解决方案< S>
{}

几点意见:这是非常抽象的例子,所以不容易想一想。铺设代码,所以你不需要滚动是很好的。这里有很多继承,可能是组成而不是扩展 TreeSet 。很难区分标识符解决方案解决方案


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". I have read previous questions about the same error but I can't solve it for my case. Could anybody help me to improve my knowledge about generics? Any reference to a good book (I can find in google a lot of information but if someone can reccommend a book, tutorial, etc. will be welcome). Although I tried to keep in mind the rules to ask a question but I apologize if my question doesn't fulfill these rules.

I have the following classes and interfaces:



public interface Subset<T extends Comparable<T>> extends Comparable<Subset<T>>
public class MathSubset<T extends Comparable<T>> extends TreeSet<T> implements Subset<T>

public interface Solution<T extends Comparable<T>>

public interface Solutions<S extends Solution<?>> extends Iterable<S>
public class SolutionsSubset<S extends Solution<?>> extends MathSubset<S> implements Solutions<S>


I need that Subset extends Comparable. In SolutionsSubset, the class MathSubset stores Solution objects. How do I have to change these definition to make it work?

Thanks you in advance

解决方案

In order to be used as the type argument in MathSubset, SolutionsSubsets S must extend Comparable<S>. As a compilable example:

import java.util.TreeSet;

interface Subset<T extends Comparable<T>>
     extends Comparable<Subset<T>> { }

class MathSubset<T extends Comparable<T>>
    extends TreeSet<T>
    implements Subset<T>
{
    public int compareTo(Subset<T> other) { throw new Error(); }
}

interface Solution<T extends Comparable<T>> { }

interface Solutions<S extends Solution<?>> extends Iterable<S> { }

class SolutionsSubset<S extends Solution<?> & Comparable<S>>
    extends MathSubset<S>
    implements Solutions<S>
{ }

A few comments: This is very abstract example, and so not easy to think about. Laying out the code so you don't need to scroll is good. There's an awful lot of inheritance going on here, perhaps compose rather than, say, extending TreeSet. It's difficult to distinguish between the identifiers Solutions and Solution.

这篇关于关于使用Java泛型的错误:“类型参数S不在其绑定范围内”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆