【问题标题】:Bounding type parameters of generic methods with interfaces?使用接口绑定泛型方法的类型参数?
【发布时间】:2015-06-02 15:01:09
【问题描述】:

所以我有两个版本的相同方法。

版本 1:

public static <T> int countGreaterThan(T[] anArray, T elem) 
    {
        int count = 0; 
        for (T e : anArray)
        {
            if (e > elem)
                count++;
        }
        return count;
    }

版本 2:

public static <T extends Comparable<T>> int countGreaterThan(T[] anArray, T elem)
    {
        int count = 0;
        for (T e : anArray)
        {
            if (e.compareTo(elem) > 0)
                count++;
        }
        return count;
    }

Eclipse 抱怨 Version 1 因为 > 运算符只能在比较原语时使用,这对我来说很有意义。

为了解决这个问题,互联网告诉我使用 Comparable 接口限定的类型参数。这就是我开始失去对正在发生的事情的掌握的地方......

从我对接口的基本理解来看,实现接口的类必须为接口中声明的每个方法提供一个方法体。

那么,为什么第 2 版不必像这样?

public int compareTo(T o)
    {
        //stuff for method body
    }


    public static <T extends Comparable<T>> int countGreaterThan(T[] anArray, T elem)
    {
        int count = 0;
        for (T e : anArray)
        {
            if (e.compareTo(elem) > 0)
                count++;
        }
        return count;
    }

^我知道这不是正确的语法,但我这样做只是为了说明我的问题,即为什么我不必为 Comparable 接口中的方法编写方法体在这种情况下。

请尽量保持通俗易懂的解释。我一直在自学这些东西,所以当我进一步研究主题时,简单的解释有助于我理解主题的技术方面。


对不起,让我澄清一下。

这是 Comparable 接口的代码:

public interface Comparable<T> {
   public int compareTo(T o);
     }

compareTo() 没有方法体,因为它是一个接口。为什么我不必手动为 compareTO() 编写一个主体,以便我可以在我的 countGreaterThan() 方法中使用该接口?

是否因为该接口是 Java Collections Framework 的一部分(如果这就是原因,请解释它是如何工作的)

这是我创建自己的界面的另一种情况:

public interface Dance { //an interface
  public void boogie(int count);
}

为了在不同的类中实现该接口,我需要在这些类中为舞蹈接口中的方法编写方法体。

public class theMoonwalk implements Dance {
  public void boogie(int count) {
    System.out.println("Slide " + count + " times!");
  }
  public void mJ() {
    System.out.println("Michael Jackson did this dance!");

}
public class theHustle implements Dance {
  public void boogie(int steps) {
    System.out.println("Step " + steps + " times!");
  }
}
public class theJitterBug implements Dance {
  public void boogie(int twists) {
    System.out.println("Twist " + twists + " times!");
  }
}

为什么我不必为 compareTo() 编写方法体(因为 compareTo() 的 Comparable 接口中不包含方法体)?

【问题讨论】:

  • 你问为什么不用写public int compareTo(T o)方法?
  • 我不明白。包含countGreaterThan 方法的类不必实现Comparable...你能澄清一下你在这里问的是什么吗?
  • @Patrick 泛型类型 T 必须实现 compareTo 方法,而不是包含泛型方法的类。
  • 这不是重复的。

标签: java generics methods interface generic-method


【解决方案1】:

T 最终引用的类型必须实现 Comparable&lt;T&gt;,而不是您声明绑定类型的类。

为了简单一点:为了使用您的 countGreaterThan 方法,您的数组和 elem 参数中包含的任何对象都必须是 Comparable 对象。

这意味着这样的调用是可以接受的:

Integer[] foo = {1, 2, 3, 4, 5};
YourClass.countGreaterThan(foo, 2);

您的类型绑定到IntegerInteger implements Comparable&lt;Integer&gt;

这是可接受的:

Object[] bar = {new Object(), new Object(), new Object()};
YourClass.countGreaterThan(bar, new Object());

您的类型绑定到Object,而Object 没有实现Comparable。相同的逻辑适用于您实现的自定义对象;如果它没有绑定到 Comparable,那么它就不会在正确的范围内。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多