我正在使用泛型编写一些类,但找不到解决方案
对于 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
发布评论
评论(3)
为了用作
MathSubset
中的类型参数,SolutionsSubset
的S
必须扩展 Comparable
。作为一个可编译的示例:一些评论:这是一个非常抽象的示例,因此不容易思考。布局代码以便您不需要滚动是很好的。这里发生了大量的继承,也许是组合而不是扩展
TreeSet
。很难区分标识符Solutions
和Solution
。In order to be used as the type argument in
MathSubset
,SolutionsSubset
sS
mustextend Comparable<S>
. As a compilable example: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 identifiersSolutions
andSolution
.泛型很快就会失控,尤其是当您试图同时“全部通用”时。少即是多。对我总是有帮助的是从具体开始(包括实现),然后慢慢地用通用参数替换,一次一个参数和一个类。
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.
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.
首先,这是完整的错误(特定于 MathSubset 未获取正确的参数):
绑定不匹配:类型 S 不是有界参数> 的有效替代品QifFixer.MathSubset
类型的问题是 MathSubset 需要一个
,但您给它一个S extends Solution< ?>
- 这些类型彼此无关,因为解决方案不会继承或实现Comparable
。如果有的话,你可以尝试这个:
不幸的是,这仍然不起作用,因为 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 aS extends Solution<?>
- those types having nothing to do with each other, because a Solution does not inherit or implementComparable<T>
.If anything, you could try this:
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?