比较泛型类型 T 和 Object

huangapple go评论105阅读模式
英文:

Comparing generic type T to Object

问题

我有以下的类:

public class SkipListSet<T extends Comparable<T>> implements SortedSet<T> {

我正在使用一个内部包装类:

class Node<T extends Comparable<T>> {

由于我正在实现SortedSet接口,我开始着手处理所需的类。

但是,contains方法的参数是Object,我想知道为什么我不能将其更改为T target。如果我这样做,我会得到以下错误:

The method contains(T) of type SkipListSet<T> must override or implement a supertype method

  1. @Override
  2. public boolean contains(Object target) {
  3. ArrayList<Node<T>> lastNodeSeen = new ArrayList<Node<T>>();
  4. Node<T> current;
  5. // 从头部的顶层开始
  6. current = header.getLevel(maxHeight);
  7. while (current.nextNode.data != target) {
  8. // 没有引用到其他节点
  9. if (current.nextNode == null) {
  10. // 退后不是一个选项,我们必须深入挖掘
  11. if (current.prevNode == null)
  12. {
  13. current = current.getLevel(current.getHeight() - 1);
  14. }
  15. // 我们必须后退
  16. current = current.prevNode;
  17. }
  18. // 如果
  19. else if (current.nextNode.data.compareTo(target) > 0) { // 如果 data > target,那么返回1
  20. }
  21. }
  22. return false;
  23. }

为什么会出现这种情况呢?

英文:

I have the following class

public class SkipListSet<T extends Comparable<T>> implements SortedSet<T> {

I am using an internal wrapper class:

class Node<T extends Comparable<T>> {

Since I am implementing the SortedSet interface, I am starting to tackle the required classes.

Though, contains has Object as an argument, I wonder why I can't change this to T target. If I do I get the following error:

  1. The method contains(T) of type SkipListSet<T> must override or implement a supertype method
  1. @Override
  2. public boolean contains(Object target) {
  3. ArrayList <Node<T>> lastNodeSeen = new ArrayList<Node<T>>();
  4. Node<T> current;
  5. // Start at top of header
  6. current = header.getLevel(maxHeight);
  7. while (current.nextNode.data != target) {
  8. // No reference to other node
  9. if (current.nextNode == null) {
  10. // retreat is not an option, we must dig deeper
  11. if (current.prevNode == null)
  12. {
  13. current = current.getLevel(current.getHeight() - 1);
  14. }
  15. // we must retreat
  16. current = current.prevNode;
  17. }
  18. // If
  19. else if (current.nextNode.data.compareTo(target) > 0) { // if data > target, then 1
  20. }
  21. }
  22. return false;
  23. }

Why is this the case?

答案1

得分: 1

SortedSet 接口扩展了 Set 接口,该接口定义了以下形式的 contains 方法:

  1. boolean contains(Object o);

该参数明确指定为一个 Object,这就是为什么在覆盖时无法将其更改为 T

英文:

The SortedSet interface extends the Set interface, which defines the method contains like so:

  1. boolean contains(Object o);

The parameter is explicitly specified to be an Object, which is why you can't change it to T when overriding it.

答案2

得分: 0

我找到了解决这个问题的方法,就是通过将目标对象进行类型转换。像这样:

  1. T newTarget = (T)(target);

你会得到以下警告:

  1. 类型安全性ObjectT的未经检查的转换

但对于我的目的,我不介意它。

英文:

I figure out a way to solve this problem is by typecasting Object target.

Like so:

T newTarget = (T)(target);

You'll get the following warning:

  1. Type safety: Unchecked cast from Object to T

But for my purposes I don't mind it.

huangapple
  • 本文由 发表于 2020年7月25日 00:50:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63078026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定