是什么

时间:2012-10-17 06:28:48

标签: java generics

我见过如下所示的方法:

protected <T extends ABC> T save( T Acd, boolean en) {

它做什么?在Java中调用的这些类型的方法声明是什么?

7 个答案:

答案 0 :(得分:35)

它被称为通用方法。这整个概念在Java中称为“泛型”。该声明意味着T可以是ABC的子类的任何类型。

答案 1 :(得分:14)

有界类型参数:

有时您可能希望限制允许传递给类型参数的类型。例如,对数字进行操作的方法可能只想接受Number或其子类的实例。这是有界类型参数的用途。

要声明有界类型参数,请列出类型参数的名称,然后是extends关键字,后跟其上限。 例如:

以下示例说明了扩展在一般意义上如何用于表示“扩展”(如在类中)或“实现”(如在接口中)。此示例是返回三个Comparable对象中最大的对象的Generic方法:

public class MaximumTest
{
   // determines the largest of three Comparable objects
   public static <T extends Comparable<T>> T maximum(T x, T y, T z)
   {                      
      T max = x; // assume x is initially the largest       
      if ( y.compareTo( max ) > 0 ){
         max = y; // y is the largest so far
      }
      if ( z.compareTo( max ) > 0 ){
         max = z; // z is the largest now                 
      }
      return max; // returns the largest object   
   }
   public static void main( String args[] )
   {
      System.out.printf( "Max of %d, %d and %d is %d\n\n", 
                   3, 4, 5, maximum( 3, 4, 5 ) );

       System.out.printf( "Maxm of %.1f,%.1f and %.1f is %.1f\n\n",
                   6.6, 8.8, 7.7, maximum( 6.6, 8.8, 7.7 ) );

       System.out.printf( "Max of %s, %s and %s is %s\n","pear",
         "apple", "orange", maximum( "pear", "apple", "orange" ) );
   }
}

答案 2 :(得分:3)

这意味着您必须发送ABC个对象或ABC的子级,不允许其他类。此外,您的Acd变量可以使用ABC类中的方法,这些方法对于包含save方法的类是可见的。

T类扩展接口时,这很有用。例如,您正在创建一个处理对象数组排序的类,此类必须实现t Comparable接口,否则将不允许该数组:

class Class1 implements Comparable<Class1> {
    //attributes, getters and setters...
    int x;

    //implementing the interface...
    public int compareTo(Class1 c1) {
        //nice implementation of compareTo
        return (this.x > c1.x)? 1 : (this.x < c1.x) ? 0 : -1;
    }
}

class Class2 {
    int x;
}

public class Sorter<T extends Comparable<T>> {

    public static void insertionSort(T[] array) {
        //good implementation of insertion sort goes here...
        //just to prove that you can use the methods of the Comparable interface...
        array[0].compareTo(array[1]);
    }

    public static void main(String[] args) {
        Class1[] arrC1 = new Class1[5];
        Class2[] arrC2 = new Class2[5];
        //fill the arrays...
        insertionSort(arrC1); //good!
        insertionSort(arrC2); //compiler error!
    }
}

答案 3 :(得分:3)

这是一个save method,除了参数T和布尔类型,其中T必须由ABC Class上限。 ABC类或任何子类都将被接受。

答案 4 :(得分:3)

这在Java中称为泛型。

Official explanation

  

简而言之,泛型使类型(类和接口)在定义类,接口和方法时成为参数。与方法声明中使用的更熟悉的形式参数非常相似,类型参数提供了一种使用不同输入重用相同代码的方法。不同之处在于形式参数的输入是值,而类型参数的输入是类型。

非正式:

像Java这样的强类型语言会导致在编译时出现更多错误而不是运行时错误。这是一件好事。但它会导致代码重复。为了缓解这种泛型,Java被添加到了Java中。

答案 5 :(得分:2)

这是泛型。类型边界的泛型!

See here for refernce

答案 6 :(得分:0)

protected <T extends ABC> T save( T Acd, boolean en) {
    // ...
}

在此功能中,我们要注意两个地方

  • 有界类型参数<T extends ABC>
  • 返回类型T

基于这些,我可以按照以下方式回答您的问题

  

它是做什么的?

save()是一种通用方法,它返回类型为T的值。 T是通用类型,仅限于ABCT的范围限于save()

  

在Java中调用的这些方法声明是什么类型?

IMO,答案应该是有界类型参数,而不是泛型。有关Java中的泛型的更多信息,您可以找到here

我想自己补充一个问题: 我们为什么要这样?

  

有时您可能希望限制可以用作参数化类型中的类型参数的类型。例如,对数字进行操作的方法可能只希望接受Number或其子类的实例。这就是 [1]的有界类型参数。

相关问题