一、泛型的概述
二、使用泛型的好处
创建集合对象,使用泛型
好处
1、避免了类型转换的麻烦,存储的是什么类型,取出的就是什么类型
2、把运行期异常(代码运行之后会抛出的异常),提升到了编译期异常(写代码的时候会报错
弊端
泛型是什么类型,只能存储什么类型的数据
创建集合对象,不使用泛型
好处:
集合不使用泛型,默认类型就是Object类型,可以存储任意类型的数据
弊端:
不安全,会发生异常
package Day13_Demo01;
import java.util.ArrayList;
import java.util.Iterator;
public class Demo01Generic {
public static void main(String[] args) {
show02();
}
/*
创建集合对象,使用泛型
好处
1、避免了类型转换的麻烦,存储的是什么类型,取出的就是什么类型
2、把运行期异常(代码运行之后会抛出的异常),提升到了编译期异常(写代码的时候会报错
弊端
泛型是什么类型,只能存储什么类型的数据
*/
private static void show02() {
ArrayList <String>list=new ArrayList();
list.add("abc");
//list.add(1);//the type ArrayList<String> is not applicable for the arguments (int)不可以在String泛型中使用int类型数据
//使用迭代器遍历list集合
Iterator<String> it=list.iterator();
while(it.hasNext()) {
//取出元素也是Object类型
String s=it.next();
System.out.println(s+"s->"+s.length());
}
}
/*
创建集合对象,不使用泛型
好处:
集合不使用泛型,默认类型就是Object类型,可以存储任意类型的数据
弊端:
不安全,会发生异常
*/
private static void show01() {
ArrayList list=new ArrayList();
list.add("abc");
list.add(1);
//使用迭代器遍历list集合
//获取迭代器
Iterator it=list.iterator();
//使用迭代器中的方法hasNext和next遍历集合
while(it.hasNext()) {
//取出元素也是Object类型
Object obj=it.next();
System.out.println(obj);
//想要使用String类特有的方法,Length获取字符串的长度;不能使用多态Object obj="abc";
//需要向下转型
//会抛出ClassCastException类型转换异常,不能将Integer类型转换成String类型
String s=(String)obj;
System.out.println(s.length());
}
}
}
三、泛型的定义和使用
1、定义和使用含有泛型的类
定义格式:
修饰符 class 类名<代表泛型的变量>{ }
package Day13_Demo01;
/*
定义一个含有泛型的类,模拟ArrayList集合
泛型是一个未知的数据类型,当我们不确定什么什么数据类型的时候,可以使用泛型
泛型可以接收任意的数据类型,可以使用Integer,String,Student...
创建对象的时候确定泛型的数据类型
*/
public class GenericClass<E> {
private E name;
public E getName() {
return name;
}
public void setName(E name) {
this.name = name;
}
}
package Day13_Demo01;
public class Demo02GenericClass {
public static void main(String[] args) {
//不写泛型默认为Object类型
GenericClass gc=new GenericClass();
gc.setName("只能是字符串");
Object obj=gc.getName();
//创建GenericClass对象,泛型使用Integer类型
GenericClass<Integer> gc2=new GenericClass<>();
gc2.setName(1);
Integer name=gc2.getName();
System.out.println(name);
//创建GenericClass对象,泛型使用String类型
GenericClass<String> gc3=new GenericClass<>();
gc3.setName("小明");
String name1=gc3.getName();
System.out.println(name1);
}
}
2、定义和使用含有泛型的方法
定义格式:
修饰符<代表泛型的变量>返回值类型 方法名(参数){ }
定义含有泛型的方法:泛型定义在方法的修饰符和返回值类型之间
格式:
修饰符<泛型>返回值类型 方法名(参数列表(使用泛型)){
方法体;
}
含有泛型的方法,在调用方法的时候确定泛型的数据类型
传递什么类型的参数,泛型就是什么类型
有普通方法和静态方法(看代码实例)
package Day13_Demo01;
/*
定义含有泛型的方法:泛型定义在方法的修饰符和返回值类型之间
格式:
修饰符<泛型>返回值类型 方法名(参数列表(使用泛型)){
方法体;
}
含有泛型的方法,在调用方法的时候确定泛型的数据类型
传递什么类型的参数,泛型就是什么类型
*/
public class GenericMethod {
//定义一个含有泛型的方法
public<M> void method01(M m) {
System.out.println(m);
}
//定义一个含有泛型的静态方法
public static <S>void method02(S s){
System.out.println(s);
}
}
测试代码:
package Day13_Demo01;
/*
测试含有泛型的方法
*/
public class Demo03GenericMethod {
public static void main(String[] args) {
//创建GenericMethod对象
GenericMethod gm=new GenericMethod();
/*
调用含有泛型的方法method01
传递什么类型,泛型就是什么类型
*/
gm.method01(1);
gm.method01('a');
gm.method01("abc");
gm.method02("静态方法不建议创建对象使用");
//静态方法,通过类名.方法名(参数)可以直接使用
GenericMethod.method02("静态方法");
GenericMethod.method02(1);
}
}
3、定义和使用含有泛型的接口
含有泛型的接口,第一种使用方式:定义接口的实现类,实现接口,指定接口的泛型
public interface Iterator<E>{
E next();
}
Scanner类实现了 Iterator接口,并且指定接口的泛型为String,所以重写的next方法泛型默认为String
public final class Scanner implements Iterator<String>{
}
含有泛型的接口第二种使用方式:接口使用什么泛型,实现类就使用什么泛型,类跟着接口走
就相当于定义了一个含有泛型的类,创建对象的时候确定泛型的类型
public interface List<E>{
boolean add(E e)
E get(int index)
public class ArrayList<E> implements List<E>
{
public boolean add(E e)
public E get(int index)
}
package Day13_Demo01;
/*
定义含有泛型的接口
*/
public interface GenericInterface<I> {
public abstract void method(I i);
}
package Day13_Demo01;
/*
含有泛型的接口,第一种使用方式:定义接口的实现类,实现接口,指定接口的泛型
public interface Iterator<E>{
E next();
}
Scanner类实现了 Iterator接口,并且指定接口的泛型为String,所以重写的next方法泛型默认为String
public final class Scanner implements Iterator<String>{
}
*/
public class GenericInterfaceImpl1 implements GenericInterface<String> {
@Override
public void method(String s) {
System.out.println(s);
}
}
package Day13_Demo01;
/*
含有泛型的接口第二种使用方式:接口使用什么泛型,实现类就使用什么泛型,类跟着接口走
就相当于定义了一个含有泛型的类,创建对象的时候确定泛型的类型
public interface List<E>{
boolean add(E e)
E get(int index)
public class ArrayList<E> implements List<E>
{
public boolean add(E e)
public E get(int index)
}
*/
public class GenericInterfaceImpl2<I> implements GenericInterface<I> {
@Override
public void method(I i) {
System.out.println(i);
}
}
package Day13_Demo01;
/*
测试含有泛型的接口
*/
public class Demo04GenericInterface {
public static void main(String[] args) {
//创建GenericInterfaceImpl1 对象
GenericInterfaceImpl1 gi1=new GenericInterfaceImpl1 ();
gi1.method("字符串");
//创建 GenericInterfaceImpl2对象
GenericInterfaceImpl2<Integer>gi2=new GenericInterfaceImpl2<>();
gi2.method(10);
GenericInterfaceImpl2<Double>gi3=new GenericInterfaceImpl2<>();
gi3.method(8.8);
}
}