泛型

来源:互联网 发布:移动宽带网络设置 编辑:程序博客网 时间:2024/04/30 06:00

----------- android培训、java培训、java学习型技术博客、期待与您交流! ------------

1、泛型的由来:



如果我们只写一个排序方法,就能够对整形数组、字符串数组甚至支持排序的任何类型的数组进行排序,这该多好啊。
Java泛型方法和泛型类支持程序员使用一个方法指定一组相关方法,或者使用一个类指定一组相关的类型。
Java泛型(generics)是JDK1.5中引入的一个新特性,泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型。
使用Java泛型的概念,我们可以写一个泛型方法来对一个对象数组排序。然后,调用该泛型方法来对整型数组、浮点数数组、字符串数组等进行排序。


2、泛型的特点
  2.1提高了程序的安全性
  2.2将运行期遇到的问题转移到了编译期
  2.3省去了类型强转的麻烦
  2.4泛型类的出现优化了程序设计


3、泛型方法的定义规则


3.1、所有泛型方法声明都有一个类型参数声明部分(由尖括号分隔),该类型参数声明部分在方法返回类型之前(在下面例子中的<E>)。


3.2、每一个类型参数声明部分包含一个或多个类型参数,参数间用逗号隔开。一个泛型参数,也被称为一个类型变量,是用于指定一个泛型类型名称的标识符。


3.3、类型参数能被用来声明返回值类型,并且能作为泛型方法得到的实际参数类型的占位符。


3.4、泛型方法方法体的声明和其他方法一样。注意类型参数只能代表引用型类型,不能是原始类型(像int,double,char的等)。


4、泛型的格式


  通过<>来定义要操作的引用数据类型。


5、在使用java提供的对象时,什么时候写泛型呢?
  通常在集合框架中很常见,只要见到<>就要定义泛型。
  其实<> 就是用来接收类型的。
  当使用集合时,将集合中要存储的数据类型作为参数传递到<>中即可。


实例:


[java] view plain copy 在CODE上查看代码片派生到我的代码片
public class GenericMethodTest  
{  
   // 泛型方法 printArray                           
   public static < E > void printArray( E[] inputArray )  
   {  
      // 输出数组元素              
         for ( E element : inputArray ){          
            System.out.printf( "%s ", element );  
         }  
         System.out.println();  
    }  
  
    public static void main( String args[] )  
    {  
        // 创建不同类型数组: Integer, Double 和 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  ); // 传递一个整型数组  
  
        System.out.println( "\nArray doubleArray contains:" );  
        printArray( doubleArray ); // 传递一个双精度型数组  
  
        System.out.println( "\nArray characterArray contains:" );  
        printArray( charArray ); // 传递一个字符型型数组  
    }   
}  
运行结果:
[java] view plain copy 在CODE上查看代码片派生到我的代码片
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  


[java] view plain copy 在CODE上查看代码片派生到我的代码片
public class MaximumTest  
{  
   // 比较三个值并返回最大值  
   public static <T extends Comparable<T>> T maximum(T x, T y, T z)  
   {                       
      T max = x; // 假设x是初始最大值  
      if ( y.compareTo( max ) > 0 ){  
         max = y; //y 更大  
      }  
      if ( z.compareTo( max ) > 0 ){  
         max = z; // 现在 z 更大             
      }  
      return max; // 返回最大对象  
   }  
   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" ) );  
   }  
}  
运行结果:
[java] view plain copy 在CODE上查看代码片派生到我的代码片
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  


6、泛型类
   泛型类的声明和非泛型类的声明类似,除了在类名后面添加了类型参数声明部分。
和泛型方法一样,泛型类的类型参数声明部分也包含一个或多个类型参数,参数间用逗号隔开。一个泛型参数,也被称为一个类型变量,是用于指定一个泛型类型名称的标识符。因为他们接受一个或多个参数,这些类被称为参数化的类或参数化的类型。
[java] view plain copy 在CODE上查看代码片派生到我的代码片
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\n\n", integerBox.get());  
     System.out.printf("String Value :%s\n", stringBox.get());  
  }  
}  
运行结果:
[java] view plain copy 在CODE上查看代码片派生到我的代码片
Integer Value :10  
   

String Value :Hello World  


----------- android培训、java培训、java学习型技术博客、期待与您交流! ------------

0 0
原创粉丝点击