泛型是程序设计语言的一种特性,允许程序员在强类型程序设计语言中编写代码时定义一些可变部分,那些部分在使用前必须作出指明。
前言
泛型如何用? 泛型的类? 泛型的方法? 泛型的接口?一、泛型是什么?
泛型是程序设计语言的一种特性。允许程序员在强类型程序设计语言中编写代码时定义一些可变部分,那些部分在使用前必须作出指明。各种程序设计语言和其编译器、运行环境对泛型的支持均不一样。将类型参数化以达到代码复用提高软件开发工作效率的一种数据类型。泛型类是引用类型,是堆对象,主要是引入了类型参数这个概念。
二、定义和使用含有泛型的类
创建一个含有泛型的类 格式为
class 类名<代表泛型的变量> {
private 泛型标识 成员名
}
代码如下(示例):
public class GenericClass<E> {
private E A;
public E getA() {
return A;
}
public void setA(E A) {
this.A = A;
}
}
再创建对象
public class TextGenericClass {
public static void main(String[] args) {
//不写泛型默认为Object类型
GenericClass GenericClass = new GenericClass();
GenericClass.setA("字符");
Object obj = GenericClass.getA();
//创建GenericClass对象,泛型使用Integer类型
GenericClass<Integer> gc1 = new GenericClass<>();
gc1.setA(1);
Integer name = gc1.getA();
System.out.println(name);
//创建GenericClass对象,泛型使用String类型
GenericClass<String> gc2 = new GenericClass<>();
gc2.setA("小明");
String name1 = gc2.getA();
System.out.println(name1);
}
}
三、定义和使用含有泛型的方法
定义含有泛型的方法:泛型定义在方法的修饰符和返回值类型之间
格式:
修饰符 <泛型> 返回值类型 方法名(参数列表(使用泛型)){
方法体;
}
含有泛型的方法,在调用方法的时候确定泛型的数据类型
传递什么类型的参数,泛型就是什么类型
代码如下(示例):
public class TextGenericMethod {
//定义一个含有泛型的方法
public <E> void method( E e)
{
System.out.println(e);
}
//定义一个含有泛型的静态方法
public static <S> void method02(S s)
{
System.out.println(s);
}
}
再创建对象
public class Method {
public static void main(String[] args) {
//使用泛型方法,参数传什么就变成什么类型
TextGenericMethod TGM = new TextGenericMethod();
TGM.method(10);
TGM.method(0.10);
TGM.method(10.35849846565);
TGM.method(10.358984444894);
TGM.method("666");
TGM.method02("静态方法,不建议创建对象使用");
//使用泛型静态方法
//静态方法,通过类名.方法名(参数)可以直接使用
TextGenericMethod.method02("这是一个静态方法");
TextGenericMethod.method02(101010);
}
}
三、定义和使用含有泛型的接口
第一种使用方式接口的实现类
含有泛型的接口,第一种使用方式:定义接口的实现类,实现接口,指定接口的泛型
例:
public interface Iterator {
E next();
}
1.定义含有泛型的接口
public interface GenericInterface<I> {
public abstract void method(I i);
}
2.实现类实现接口
ublic class GenericInterfaceImpl1 implements GenericInterface<String> {
@Override
public void method(String s) {
}
}
3.创建对象
public static void main(String[] args) {
GenericInterfaceImpl1 gi1 = new GenericInterfaceImpl1();
gi1.method("字符串");
gi1.method("55");
}
}
第二种使用方式接口的实现类
含有泛型的接口第二种使用方式:接口使用什么泛型,实现类就使用什么泛型,类跟着接口走
就相当于定义了一个含有泛型的类,创建对象的时候确定泛型的类型
例子:
public interface List{
boolean add(E e);
E get(int index);
}
代码如下(示例):
public class GenericInterfaceImpl2<I> implements GenericInterface<I> {
@Override
public void method(I i) {
System.out.println(i);
}
}
public static void main(String[] args) {
GenericInterfaceImpl1 gi1 = new GenericInterfaceImpl1();
gi1.method("字符串");
gi1.method("55");
//创建GenericInterfaceImpl2对象
GenericInterfaceImpl2<Integer> gi2 = new GenericInterfaceImpl2<>();
gi2.method(10);
GenericInterfaceImpl2<Double> gi3 = new GenericInterfaceImpl2<>();
gi3.method(2.22);
}
}
四、泛型的通配符
泛型的通配符:
?:代表任意的数据类型
使用方式:
不能创建对象使用
只能作为方法的参数使用
不知道使用什么类型来接收的时候,此时可以使用?,?表示未知通配符。
此时只能接受数据,不能往该集合中存储数据
代码如下(示例):
public class 泛型的通配符 {
public static void main(String[] args) {
ArrayList<Integer> list1 = new ArrayList<>();
list1.add(1);
list1.add(2);
ArrayList<String> list2 = new ArrayList<>();
list2.add("a");
list2.add("b");
/*
定义一个方法,能遍历所有类型的ArrayList集合
这时候我们不知道ArrayList集合使用什么数据类型,可以泛型的通配符?来接收数据类型
注意:
泛型没有继承概念的
只能用来传参数不能存储
*/
public static void printArray(ArrayList<?> list) //加通配符,所以可以接收字符和整型
{
//使用迭代器遍历集合
Iterator<?> it = list.iterator();
while (it.hasNext())//遍历输出
{
//***it.next()方法,取出的元素是Object,可以接收任意的数据类型
Object o = it.next();
System.out.println(o);
}
}
五、通配符高级使用----受限泛型
设置泛型的时候,实际上是可以任意设置的,只要是类就可以设置。但是在JAVA的泛型中可以指定一个泛型的上限和下限。
泛型的上限:
- 格式:
类型名称 <? extends 类 > 对象名称
- 意义:
只能接收该类型及其子类
泛型的下限:
- 格式:
类型名称 <? super 类 > 对象名称
- 意义:
只能接收该类型及其父类型
比如:现已知Object类,String 类,Number类,Integer类,其中Number是Integer的父类
~~~java
public static void main(String[] args) {
Collection<Integer> list1 = new ArrayList<Integer>();
Collection<String> list2 = new ArrayList<String>();
Collection<Number> list3 = new ArrayList<Number>();
Collection<Object> list4 = new ArrayList<Object>();
getElement(list1);
getElement(list2);//报错
getElement(list3);
getElement(list4);//报错
getElement2(list1);//报错
getElement2(list2);//报错
getElement2(list3);
getElement2(list4);
}
// 泛型的上限:此时的泛型?,必须是Number类型或者Number类型的子类
public static void getElement1(Collection<? extends Number> coll){}
// 泛型的下限:此时的泛型?,必须是Number类型或者Number类型的父类
public static void getElement2(Collection<? super Number> coll){}