问题描述
解决方案基础类型的$ c> 课程。因此 K 必须扩展可比较<可锁定<课程>> &安培;可设置关键点<课程和GT; 。但它没有,它扩展了 Comparable< Keyable< DataElement>> &安培; Keyable< DataElement> 。
我想 DataElement 应该类似于 Comparable 或 Enum 。
public interface Keyable< T> {public String getKey();}
public interface DataElement< THIS extends DataElement< THIS>>可扩展<可关联< THIS>>,可键< THIS>,可序列化{...}
公共类课程实现DataElement< Course> {...}
public interface SearchTree< K extends Comparable< Keyable< K>> &安培;可设置关键点< K>>扩展Serializable {...}
public class MySearchTree实现SearchTree< Course> {
I'm working on a project for class that involves generics.
public interface Keyable <T> {public String getKey();} public interface DataElement extends Comparable<Keyable<DataElement>>, Keyable<DataElement>, Serializable {...} public class Course implements DataElement {...} public interface SearchTree<K extends Comparable<Keyable<K>> & Keyable<K>> extends Serializable {...} public class MySearchTree implements SearchTree<Course> { ... private class Node { public Course data; public Node left; public Node right; ... } }When trying to use the Course class within the declaration of MySearchTree, I receive a type argument error stating that "Course is not within the bounds of type-variable K". I spent a good amount of time trying to figure out what attributes Course might be lacking to make it not fit the bill, but came up empty.
Any ideas?
解决方案In MySearchTree the K of the base type is Course. So K must "extend" Comparable<Keyable<Course>> & Keyable<Course>. But it doesn't, it extends Comparable<Keyable<DataElement>> & Keyable<DataElement>.
I guess DataElement should be generified in a similar manner to Comparable or Enum.
public interface Keyable <T> {public String getKey();} public interface DataElement<THIS extends DataElement<THIS>> extends Comparable<Keyable<THIS>>, Keyable<THIS>, Serializable {...} public class Course implements DataElement<Course> {...} public interface SearchTree<K extends Comparable<Keyable<K>> & Keyable<K>> extends Serializable {...} public class MySearchTree implements SearchTree<Course> {
这篇关于Java泛型问题:类“不在类型变量的范围内”错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-06 09:44