java classable_Java泛型问题:类“不在类型变量的范围内”错误。(Java generics issue: Class “not within bounds of type-varia...

Java泛型问题:类“不在类型变量的范围内”错误。(Java generics issue: Class “not within bounds of type-variable” error.)

我正在开发一个涉及泛型的类的项目。

public interface Keyable {public String getKey();}

public interface DataElement extends Comparable>, Keyable, Serializable {...}

public class Course implements DataElement {...}

public interface SearchTree> & Keyable> extends Serializable {...}

public class MySearchTree implements SearchTree {

...

private class Node {

public Course data;

public Node left;

public Node right;

...

}

}

当试图在MySearchTree的声明中使用Course类时,我收到一个类型参数错误,指出“Course不在类型变量K的范围内”。 我花了很多时间试图弄清楚课程可能缺乏什么样的特性,使得它不适合该法案,但空洞起来。

有任何想法吗?

I'm working on a project for class that involves generics.

public interface Keyable {public String getKey();}

public interface DataElement extends Comparable>, Keyable, Serializable {...}

public class Course implements DataElement {...}

public interface SearchTree> & Keyable> extends Serializable {...}

public class MySearchTree implements SearchTree {

...

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?

原文:https://stackoverflow.com/questions/9968687

更新时间:2019-06-11 21:35

最满意答案

在MySearchTree中,基本类型的K是Course 。 所以K必须“扩展” Comparable> & Keyable 。 但它没有,它扩展了Comparable> & Keyable 。

我猜DataElement应该以类似于Comparable或Enum方式进行基因化。

public interface Keyable {public String getKey();}

public interface DataElement> extends Comparable>, Keyable, Serializable {...}

public class Course implements DataElement {...}

public interface SearchTree> & Keyable> extends Serializable {...}

public class MySearchTree implements SearchTree {

In MySearchTree the K of the base type is Course. So K must "extend" Comparable> & Keyable. But it doesn't, it extends Comparable> & Keyable.

I guess DataElement should be generified in a similar manner to Comparable or Enum.

public interface Keyable {public String getKey();}

public interface DataElement> extends Comparable>, Keyable, Serializable {...}

public class Course implements DataElement {...}

public interface SearchTree> & Keyable> extends Serializable {...}

public class MySearchTree implements SearchTree {

2016-03-10

相关问答

在MySearchTree中,基本类型的K是Course 。 所以K必须“扩展” Comparable> & Keyable 。 但它没有,它扩展了Comparable> & Keyable 。 我猜DataElement应该以类似于Comparable或Enum方式进行基因化。 public interface Keyable {public String getKey()

...

GroupOfParts的第二个通用参数必须是PartDecorator> PartDecorator> 。 而且由于泛型默认不变,因此不会有偏差。 但是GroupOfPartsDecorImpl使用PartDecorator> ,这是不一样的,因此它不能编译。 您可以通过在MainContainerGroupPartDecorator的声明MainContainerGroupPar

...

运行时的getClass()提供Class 。 由于类型擦除有关泛型参数的信息不可用。 你必须做一个演员 @SuppressWarnings("unchecked")

public ObjectReference(LoadableObjectFactory obj)

{

_cls = (Class extends LoadableObjectFactory>) obj.getClass();

}

让它工作 getClass() in runtime prov

...

类型边界 & Measurer>意味着参数扩展了Measurable AND Measurer,但你想要它是OR。 你不能用泛型参数语法做到这一点。 但是,您可以在DataSet重载方法。 public add(Measurable m) {

//...

}

public add(Measurer m) {

//...

}

或者您可以进行运行时类型检查,尽管这不是最合适的。

...

将泛型类型指定为 Map data = this.aub.getData();

data.put("ip_macs", new LinkedList()); // Compiles

当你说Map Map它表示一个Map其键的类型为String并且该值扩展了Object但其类型未知 ? 。 由于该类型未知,因此在其中插入LinkedList对象是不

...

您是在尝试创建泛型类,还是构造函数采用泛型参数的具体类? 如果您尝试创建一个泛型类,可以为扩展AppCompatActivity & DialogInterface.OnDismissListener对象实例化,则需要使该类具有通用性(如在编辑中)。 在泛型类中,类型参数不需要在构造函数定义中重复,它们由隐式编译器承担。 如果您尝试创建一个具体或泛型类的类,其构造函数可以接受扩展AppCompatActivity & DialogInterface.OnDismissListener对象,则需要一

...

您收到错误的原因是Process没有实现Comparable ,这是您作为Heap类型参数提供的所有类必须满足的条件,因为您放置在泛型类型上的类型约束。 The reason that you get the error is that Process does not implement Comparable, a condition that must be met by all classes that you supply as type argu

...

我想通过使用通用我来创建boardManager以扩展Number和Comparable来解决这个问题,如果泛型不扩展Number则会出错。 你不能这样做。 您不能在类上具有仅允许某些类型参数化的方法; 该类必须能够使用任何允许的类型参数完全运行。 您需要约束整个Board类以使T为Number 。 另一个错误是因为Class>与Class不兼容。 >表示类型参数可能是任何可能的类型; 它可能是违反T约束的东西。 你需要强制转换为Class 。 I want to fix th

...

我能想到的最明显的原因甚至不是类型擦除; 事实上,当你使A成为B的子类时,Java编译器需要知道构造函数B有什么。 如果一个类没有无参数构造函数,那么它的子类必须调用其定义的构造函数之一。 但是当你宣布时 public class MyClass extends T {...}

编译器绝对不可能知道super构造函数是什么,因为T在编译时并不是固定的(毕竟这是泛型的整个点),这是Java中不允许的情况。 The most obvious reason I can think of isn'

...

第一:方法声明 public static > void sort(List list)

对我来说没有多大意义。 我觉得它应该是 public static > void sort(List list)

然后就可以编写sort(listOfStudents) 。 现在我将解释上限和下限有界通配符的优点: 类型参数的多态性不会转移到它的泛型类型 这意味着学生List

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值