泛型
使用泛型的好处
import java.util.Iterator;
import java.util.ArrayList;
public class Demo01Feneric {
public static void main(String[]args){
show02();
}
//创建集合对象,使用泛型
//好处:1,避免了类型转换的麻烦,存储的是什么类型,取出的就是什么类型
//2,把运行期异常(代码运行之后会抛出的异常),提升到了编译期(写代码的时候会报错)
//弊端:泛型是什么类型,只能存储什么类型的数据
private static void show02(){
ArrayList<String> list = new ArrayList<String>();
list.add("abc");
// list.add(1);//add(java.lang.String)in ArrayList cannot be applied to (int)
//使用迭代器遍历list集合
Iterator<String> it = list.iterator();
while(it.hasNext()){
String s = it.next();
System.out.println(s + "->" + s.length());//abc->3
}
}
//创建集合对象,不使用泛型
//好处:
// 集合不使用泛型,默认的类型是Object类型,可以存储任意类型的数据
//弊端: 不安全,会引发异常
private static void show01(){
ArrayList list = new ArrayList();//Object,
list.add("abc");//可以添加任何数据类型
list.add(1);
//使用迭代器遍历list集合
//获取迭代器
Iterator it = list.iterator();
//迭代器的泛型跟着集合走,集合没有泛型,那么迭代器也没泛型
//使用迭代器中的方法hasNext和next遍历集合遍历集合while循环
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());
}
}
}
定义和使用含有泛型的类
定义的一个含有泛型的类,模拟ArrayList集合
泛型是一个未知的数据类型,当我们不确定什么什么数据类型的时候,可以使用泛型
泛型接收任意的数据类型,可以使用Integer,String,Student…
创建对象的时候确定泛型的数据类型
//Generic泛型的意思
public class GenericClass <E> {//添加<E>也就是含有泛型的类
private/* String*/E name;//String 换成E
public /*String*/E getName() {
return name;
}
public void setName(/*String*/E name) {
this.name = name;
}
}
实现
mport sun.misc.GC;
public class Demo02GenericClass {
public static void main(String[]args){
//不写泛型,默认为Object类型
GenericClass gc = new GenericClass();
gc.setName("只能是字符串");
Object obj = gc.getName();
//创建CenericClass对象,泛型使用Integer类型
GenericClass<Integer> gc2 = new GenericClass<Integer>();
gc2.setName(1);
Integer name = gc2.getName();//自动拆箱
System.out.println(name);//1
//创建CenericClass对象,泛型使用String类型
GenericClass<String> gc3 = new GenericClass<String>();
gc3.setName("小明");
String name1 = gc3.getName();
System.out.println(name1);//小明
// String name = gc.getName();
// System.out.println(name);//只能是字符串
}
}
定义和使用含有泛型的方法
定义含有泛型的方法:泛型定义在方法 的修饰符和返回值类型之间
格式:
修饰符 <泛型> 返回值类型 方法名称(参数列表(使用泛型)){
方法体;
}
含有泛型的方法,在调用方法的时候确定泛型的数据类型
传递什么类型的参数,泛型就是什么类型
/Method 是方法的意思
public 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 Demo03GenericMethod {
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(1);
}
}
定义和使用含有泛型的接口
含有泛型的接口:
**第一种使用方式:**定义接口的实现类,实现接口,指定接口的泛型
public interface Iterator{
E next();
}
Scanner类实现了Iterator接口,并指定接口的泛型为String, 所以重写的next方法泛型默认就是字符串
public final class Scanner implements Iterator{
public String next(){}
}
/*
定义含有泛型的接口
*/
public interface GenericInterface <I> {
//接口中定义一个抽象方法,没有返回值
public abstract void method(I i);
}
public class GenericInterfaceImpl1 implements GenericInterface<String> {//<String>类型,也可以是其它类型
public void method(String s) {
System.out.println(s);
}
}
//测试含有泛型的接口
public class Demo04GenericInterface {
public static void main(String[] args){
//创建GenericInterfaceImpl1对象
GenericInterfaceImpl1 gi1 = new GenericInterfaceImpl1();
gi1.method("字符串");
含有泛型的接口,第二种使用方式:接口使用什么泛型,实现类就使用什么泛型,类跟着接口走
就相当于定义了一个含有泛型的类,创建对象的时候确定泛型的类型
public interface List{
boolean add (E e);
E get(int index);
}
public class ArrayList implements List{
public boolean add(E e){}
public E get(int index){}
}
public class GenericInterfaceImpl2<I> implements GenericInterface<I>{
public void method(I i) {
// TODO Auto-generated method stub
}
}
public class Demo04GenericInterface {
public static void main(String[] args){
//创建GenericInterfaceImpl2对象
GenericInterfaceImpl2<Integer> gi2 = new GenericInterfaceImpl2<Integer>();
//<Integer>是整数
gi2.method(10);
GenericInterfaceImpl2<Double> gi3 = new GenericInterfaceImpl2<Double>();
//<Double>类型
gi3.method(8.8);
}
}