博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java中的泛型
阅读量:7233 次
发布时间:2019-06-29

本文共 3612 字,大约阅读时间需要 12 分钟。

以下内容引用自:

如果写一个单独的能在一个整型数组,一个字符串数组或者一个任何类型支持排序的数组内排列元素的排序方法将会是很好的。

Java Generic方法和Generic类可以用一种单独的方法声明,一系列有关的方法或者用一个单独的类声明来各自指定一系列有关的类型。

Generic也提供了编译时类型安全来允许在编译时捕获无效的类型。

使用Java Generic概念,可以写一个泛型方法来给对象数组排序,然后调用带有整型数组,double型数组,字符串数组或其他的泛型方法来为数组元素排序。

一、泛型方法

写一个单独的可以被不同类型的参数调用的泛型方法声明。基于传给泛型方法的参数类型,编译器正确处理每个调用的方法。以下是定义泛型方法的规则:

  • 所有的泛型方法声明在方法的返回值之前有一个由尖括号分隔开的类型参数区域。
  • 类型参数能被用来声明返回类型和作为传给泛型方法的参数类型的占位符,就是为人所知的真正的类型参数。
  • 一个泛型方法的主体像其他方法声明一样。注意到类型参数仅能代表引用类型,而不是原始类型(就像 int,double和char)。

示例:

以下的例子说明了可以通过一个通用的方法来打印不同类型的数组:

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( "\nArray doubleArray contains:" );        printArray( doubleArray ); // pass a Double array        System.out.println( "\nArray characterArray contains:" );        printArray( charArray ); // pass a Character array    } }//这将产生以下结果:Array integerArray contains:1 2 3 4 5 6Array doubleArray contains:1.1 2.2 3.3 4.4 Array characterArray contains:H E L L O

二、受限的类型参数

有时候想要限制允许传给一个类型参数的类型种类。例如,操作数的方法可能仅仅想要接受Number的实例或者它的子类。这就是有限的类型参数。

声明一个有限的类型参数,列举类型参数的名字,后面跟着扩展关键字和它的上界。

示例:

下面的例子说明了如何使用扩展在一定意义上意味着“继承”(在类中)或是在“实现”(在接口中)。这个例子是返回三个Comparable对象中最大的泛型方法:

public class MaximumTest{   // determines the largest of three Comparable objects   public static 
> 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" ) ); }}//这将会产生以下结果:Maximum of 3, 4 and 5 is 5Maximum of 6.6, 8.8 and 7.7 is 8.8Maximum of pear, apple and orange is pear

提示:

  • 限制条件可以有多个,中间用&符号隔开,如:T extends Number & Comparable<T>,且类优先于接口。
  • 这里的关键字extends与Java继承没关系,这个仅仅是用来做泛型的限制。

三、泛型类

泛型类的声明看起来像一个非泛型类的声明,除了类名后跟着一个类型参数区域。

与泛型方法一样,一个泛型类的类型参数区域可以拥有一个或者更多的由逗号分隔的类型参数。这些类被称为参数化的类或是参数化的类型因为他们接受一个或更多的参数。

示例:

下面的例子说明了怎样定义一个泛型类:

public class Box
{ private T t; public void add(T t) { this.t = t; } public T get() { return t; } public static void main(String[] args) { Box
integerBox = new Box
(); Box
stringBox = new Box
(); 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()); }}//这将产生以下结果:Integer Value :10String Value :Hello World

 

测试工程:

转载地址:http://nbpfm.baihongyu.com/

你可能感兴趣的文章
SQL游标循环执行(又遇到了,记录一下吧)
查看>>
jQuery上注册函数的方法
查看>>
不要将@Autowired注解用于static方法
查看>>
关于达内培训的名企定制班
查看>>
Routing with restify and socket.io in node
查看>>
立体测距
查看>>
关于离线下载的一些免费的网站
查看>>
开发netfilter的一些坑
查看>>
java中map的clear和new性能对比
查看>>
macbook 备份系统
查看>>
klish 安装与使用
查看>>
Django实战(18):提交订单
查看>>
PHP时间戳函数总结
查看>>
开发自定义JSF组件(1) HelloWorld
查看>>
设计模式学习——策略模式:模拟鸭子应用
查看>>
lucene4.7 分词器(三)
查看>>
linux下让tomcat以service方式运行(及使用service tomcat start)
查看>>
HTML模版
查看>>
观察者模式摘要
查看>>
我的友情链接
查看>>