码迷,mamicode.com
首页 > 编程语言 > 详细

Java技术_Java千百问(0041)_泛型如何使用

时间:2016-04-26 21:27:40      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:

点击进入_更多_Java千百问

1、如何使用泛型方法

了解什么是泛型看这:泛型是什么
以下是定义泛型方法的规则:

  1. 声明泛型方法时,在返回类型之前,需要有一个由尖括号(< >)分隔的泛型类型部分。
    2 . 一个泛型类型,也称为类型参数,是一个标识符,用于指定一个泛型类型的名称
  2. 类型参数可以用来声明返回类型和充当占位符传递给泛型方法。
  3. 泛型方法的身体与其他方法一样。

例子:

public class GenericMethodTest
{
   // generic method printArray                        
   public static < E > void printArray( E[] inputArray )
   {
      // Display array elements             
         for ( E element : inputArray ){       
            System.out.printf( "%s ", element );
         }
         System.out.println();
    }

    public static void main( String args[] )
    {
        // Create arrays of Integer, Double and Character
        Integer[] intArray = { 1, 2, 3, 4, 5 };
        Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };
        Character[] charArray = { ‘H‘, ‘E‘, ‘L‘, ‘L‘, ‘O‘ };

        System.out.println( "Array integerArray contains:" );
        printArray( intArray  ); // pass an Integer array

        System.out.println( "
Array doubleArray contains:" );
        printArray( doubleArray ); // pass a Double array

        System.out.println( "
Array characterArray contains:" );
        printArray( charArray ); // pass a Character array
    }
}

这将产生以下结果:
Array integerArray contains:
1 2 3 4 5 6
Array doubleArray contains:
1.1 2.2 3.3 4.4
Array characterArray contains:
H E L L O

泛型类型还可以被限制,使用extends关键字限制泛型的父类。
例子:

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

",
                   3, 4, 5, maximum( 3, 4, 5 ) );

      System.out.printf( "Maxm of %.1f,%.1f and %.1f is %.1f

",
                   6.6, 8.8, 7.7, maximum( 6.6, 8.8, 7.7 ) );

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

这将产生以下结果:
Maximum of 3, 4 and 5 is 5
Maximum of 6.6, 8.8 and 7.7 is 8.8
Maximum of pear, apple and orange is pear

2、如何使用泛型类/接口

泛型类/接口的声明与非泛型类类似,除了类名后增加了一个泛型类型。
与泛型方法相比,泛型类的类型参数部分可以用逗号分隔的一个或多个泛型类型。

例子:

public class Box<T> {

  private T t;

  public void add(T t) {
    this.t = t;
  }

  public T get() {
    return t;
  }

  public static void main(String[] args) {
     Box<Integer> integerBox = new Box<Integer>();
     Box<String> stringBox = new Box<String>();

     integerBox.add(new Integer(10));
     stringBox.add(new String("Hello World"));

     System.out.printf("Integer Value :%d

", integerBox.get());
     System.out.printf("String Value :%s
", stringBox.get());
  }
}

这将产生以下结果:
Integer Value :10
String Value :Hello World

Java技术_Java千百问(0041)_泛型如何使用

标签:

原文地址:http://blog.csdn.net/ooppookid/article/details/51233633

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!