关于使用 Java 泛型的错误:“类型参数 S 不在其范围内”

发布于 2024-10-07 13:26:58 字数 865 浏览 6 评论 0 原文

我正在使用泛型编写一些类,但找不到解决方案 对于 SolutionsSubset 类,所以我收到错误 “类型参数 S 不在其范围内”。我读过以前的 关于相同错误的问题,但我无法解决我的情况。 有人可以帮助我提高我对仿制药的了解吗?任何 参考一本好书(我在google上可以找到很多资料 但如果有人可以推荐一本书、教程等,我们将受到欢迎)。 虽然我试图记住提问的规则,但我 如果我的问题不符合这些规则,请道歉。

我有以下类和接口:



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>


我需要 Subset 扩展 Comparable。在 SolutionsSubset 中,MathSubset 类存储 Solution 对象。我必须如何更改这些定义才能使其发挥作用?

提前谢谢你

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱花坊 2024-10-14 13:26:58

为了用作 MathSubset 中的类型参数,SolutionsSubsetS 必须扩展 Comparable。作为一个可编译的示例:

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>
{ }

一些评论:这是一个非常抽象的示例,因此不容易思考。布局代码以便您不需要滚动是很好的。这里发生了大量的继承,也许是组合而不是扩展TreeSet。很难区分标识符 SolutionsSolution

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.

勿忘心安 2024-10-14 13:26:58

泛型很快就会失控,尤其是当您试图同时“全部通用”时。少即是多。对我总是有帮助的是从具体开始(包括实现),然后慢慢地用通用参数替换,一次一个参数和一个类。

有人可以帮助我提高我对泛型的了解吗?

http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html

不是教程,但有很多有用的信息。这是您阅读可以理解的部分的参考资料之一,但随着您获得更多的掌握并且更多的内容开始有意义,您将在未来一遍又一遍地回来。

Generics are something that can quickly get out of hand, especially if you try to "be all generic" all at once. Less is more. What always helps me is to start concrete (including the implementation) and then slowly substitute generic parameters in, one parameter and class at a time.

Could anybody help me to improve my knowledge about generics?

http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html

Not a tutorial, but lots of useful info. Its one of those references that you read the parts you can understand, but come back to over and over again in the future as you gain more mastery and more of it begins to make sense.

萤火眠眠 2024-10-14 13:26:58

首先,这是完整的错误(特定于 MathSubset 未获取正确的参数):绑定不匹配:类型 S 不是有界参数 > 的有效替代品QifFixer.MathSubset 类型的

问题是 MathSubset 需要一个 ,但您给它一个 S extends Solution< ?> - 这些类型彼此无关,因为解决方案不会继承或实现 Comparable

如果有的话,你可以尝试这个:

public class SolutionsSubset<S extends Comparable<S>> extends
    MathSubset<S> implements Solutions<Solution<S>>;

不幸的是,这仍然不起作用,因为 MathSubset 实现了 Iterable,但 Solutions 也是如此。

一个简单的修复方法是解决方案不扩展 Iterable,但在我看来,您确实正在尝试使用比您需要的更复杂的方法。也许“has-a”而不是“is-a”设计在这里可能更有利?

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 <T extends Comparable<T>> of the type QifFixer.MathSubset<T>

The problem is that MathSubset expects a <T extends Comparable<T>, 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<T>.

If anything, you could try this:

public class SolutionsSubset<S extends Comparable<S>> extends
    MathSubset<S> implements Solutions<Solution<S>>;

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文