点击查看:java集合(List、Set、Map)博客目录
第三节 : 泛型
3.1 、泛型概述
在前面学习集合时,我们都知道集合中是可以存放任意对象的,只要把对象存储集合后,那么这是他们会被提升成Object类型。当我们在取出每一个对象,并且进行相应的操作,这是必须采用类型转换。
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class GenericDemo{
public static void main(String []args){
show01();
show02();
}
// 创建集合对象不使用泛型,
// 好处:集合不使用泛型,默认类型就是Object类型,可以存储任意任意类型的数据
// 弊端:不安全,可能会引发异常
private static void show01(){
ArrayList list = new ArrayList();
list.add("abc");
list.add(10);
// 使用迭代器遍历list集合
//获取迭代器
Iterator it = list.iterator();
// 使用迭代器中的方法hasNext和next遍历集合。
while(it.hasNext()){
// 取出元素也是Object类型
Object obj = it.next();
System.out.println(obj); // 输出 abc /n 10
/**想要使用String类特有的方法,length()获取字符串的长度,不能使用。
* 多态 object obj = "abc";
* 需要向下转型
* 会抛出classCastException类型转换异常,不能把Integer类型转化为String类型 */
String s = (String)obj;
System.out.println(s.length()); 、、
}
}
/** 创建集合对象使用泛型
* 好处:1. 避免了类型转换的麻烦,存储的是什么类型,取出就是什么类型。
* 2. 把运行期异常(代码运行之后会抛出的异常),提升到了编译期(写代码的时候会报错)
* 3. 泛型是什么类型 , 只能存储什么类型的数据。*/
public static void show02(){
ArrayList<Stirng> list = new ArrayList<>();
list.add("abc");
// list.add(1); // add(java.lang.String) in ArrayList cannot be applied to (int)
// 使用迭代器遍历list集合
//获取迭代器
Iterator it = list.iterator();
// 使用迭代器中的方法hasNext和next遍历集合。
while(it.hasNext()){
String s = it.next();
System.out.println(s+"->"+s.length()); // 输出: abc -> 3
}
}
}
3.3.1 、 泛型的定义和使用
定义格式:
修饰符 class 类名<代表泛型的变量>{ }
例如,API中的ArrayList集合:
class ArrayList<E>{
public boolean add(E e){ }
public E get(int index){ }
}
使用泛型:即什么时候确定泛型
在创建泛型的时候确定泛型
/** 定义一个含有泛型的类,模拟ArrayList集合
* 泛型是一个未知的数据类型,当我们不确定是什么数据类型的时候,可能使用泛型
* 泛型可以接收任意的数据类型,可以使用Integer , String , Student ......*/
* import java.util.*;
class GeneericClass<E>{
private E name;
public E getName(){
return name;
}
public void setName(E name){
this.name = name;
}
}
public class Main{
public static void main(String []args){
// 不写泛型默认为: object类型
Main gc = new Main();
gc.setName("字符串");
object obj = gc.getName();
//创建GenericClass对象,泛型使用Integer类型
GeneericClass<Integer> gc2 = new GeneericClass<>();
gc2.setName(1);
Integer name = gc2.getName();
System.out.println(name);
// 创建GeneericClass对象,泛型使用String类型
GeneericClass<String> gc3 = newGeneericClass<>();
gc3.setName("小明");
String name1 = gc3.getName();
System.out.println(name1);
}
}
3.3.2 、 含有泛型的方法
定义格式:
修饰符 <代表泛型的变量> 返回值类型 方法名(参数){ }
例如,
/**定义含有泛型的方法:泛型定义在方法的修饰符和返回值类型之间
* 含有类型的方法,在调用方法的时候确定泛型的数据类型
* 传递什么类型的参数,泛型就是什么类型 */
* import java.util.*;
class GenericMethod{
// 定义一个含有泛型的方法
public <M> void method01(M m){
System.out.println(m);
}
// 定义一个含有泛型的静态方法
public static <S> void method02(S s){
System.out.println(s);
}
}
public class TestGenericMethod{
public static void main(String []args){
// 创建GenericMethod对象
GenericMethod gm = new GenericMethod();
/**调用含有泛型的方法method01
* 传递什么类型,泛型就是什么类型
*/
gm.method01(10);
gm.method01("abc");
gm.method01(8.8);
gm.method01(true);
gm.method02("静态方法,不建议创建对象使用");
// 静态方法,通过类名。方法名(参数)可以直接使用
GenericMethod.method02("静态方法");
GenericMethod.method02(10);
}
}
3.3.3 、 含有泛型的接口
定义格式:
修饰符 interface 接口名<代表泛型的变量> { }
例如,
import java.util.*;
public interface MyGenericInterface<E>{
public abstract void add(E e){
public abstract E getE();
}
}
/* 定义含有泛型的接口
*/
interface GenericInterface<I>{
public abstract void method(I i){
}
}
/* 含有泛型的接口,第一种使用方式:定义接口的实现类,实现接口,指定接口的泛型
* public interface Iterator{ E next(); }
*/
class GenericInterfaceImpl1 implement GenericInterface<String>{
@Override
public void method(String s){
System.out.println(s);
}
}
// 接口使用什么泛型 实现类就使用什么泛型,类跟着接口走
// 就相当于定义了一个含有泛型的类,创建对象的时候确定函数的类型
class GenericInterfaceImpl2 implement GenericInterface<I>{
@Override
public void method(I i){
System.out.println(i);
}
}
// 测试含有泛型的接口
public class TestGenericInterface{
public static void main(String []args){
// 创建GenericInterfaceImpl1的对象
GenericInterfaceImpl1 gi1 = new GenericInterfaceImpl();
gi1.method("字符串");
// 创建GenericInterfaceImpl2的对象
GenericInterfaceImpl2 gi2 = new GenericInterfaceImpl();
gi2.method(10);
}
}
3.4 、 泛型通配符
当使用泛型类或者接口时,传递的数据中,泛型类型不确定,可以通过通配符<?>表示。但是一旦使用泛型的通配符后,只能使用object类中的共性方法,集合中元素自身方法无法使用。
通配符基本使用
泛型的通配符:不知道使用什么类型来接收的时候,此时可以使用? , ?表示未知通配符。
此时只能接收数据,不能往该集合中存储数据。
举个栗子:
public static void main(String []args){
Collection<Integer> list1 = new ArrayList<Integer>();
getElement(list1);
Collection<Integer> list1 = new ArrayList<Integer>();
getElement(list2);
}
public static void getElement(Collection<?> coll){ }
// ?代表可以接收任意类型
import java.util.*;
public class Generic{
public static void main(String args[]){
ArrayList<Integer> list01 = new ArrayList<>();
list01.add(1);
list01.add(2);
ArrayList<String> list02 = new ArrayList<>();
list02.add("aa");
list02.add("bb");
printArray(list01);
printArray(list02);
}
/*定义一个方法,能遍历所有类型的ArrayList集合
* 这时我们不知道ArrayList集合使用什么数据类型,可以使用泛型的通配符?来接收数据。
*/
private 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 类> 对象名称
- 意义:只能接收该类型及其父类型
/**泛型的上限限定: ? extends E 代表使用的泛型只能是E类型的子类/本身
* 泛型的下限限定: ? super E 代表使用的泛型只能是E类型的父类/本身
*/
import java.util.*;
public class Main{
public static void main(Stirng 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);
}/* 报错原因:
类与类之间的继承关系:
integer extends Number extends Object
String extends Object */
// 泛型的上限:此时的泛型?,必须是Number类型或者Number类型的子类
public static void getElement(Collection<? extends Number>coll){}
// 泛型的下限:此时的泛型?,必须是Number类型或者Number类型的父类
public static void getElement(Collection<? super Number>coll){}
}