Java语言高级-04集合-第2节泛型

泛型的概念
在这里插入图片描述
使用泛型的好处
在这里插入图片描述

package demo18;

import java.util.ArrayList;
import java.util.Iterator;

public class Demo01Generic {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		show02();
	}

	/*
	创建集合对象,使用泛型
	好处:
	1/避免了类型转换,存储的是什么类型,取出的就是什么类型
	2、把运行期异常(代码运行之后会抛出异常),提升到了编译期(写代码的时候会报错)
	弊端:
	泛型是什么类型,只能存储什么类型的数据
	
	 */
	private static void show02() {
		// TODO Auto-generated method stub
		ArrayList<String> list = new ArrayList();
		list.add("abc");
		//list.add(1);//The method add(int, String) in the type ArrayList<String> is not applicable for the arguments (int)
	
		//使用迭代器遍历list集合
		Iterator<String> it = list.iterator();
		while(it.hasNext()) {
			String s = it.next();
			System.out.println(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());
		}
		
	}
	
}

定义和使用含有泛型的类

package demo18;

public class Demo02GenericClass {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		//不写泛型默认为Object类型
		GenericClass gc = new GenericClass();
		gc.setName("只能是字符串");
		Object obj = new GenericClass();

		//创建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(name);
	}

}

package demo18;

/*
定义一个含有泛型的类,模拟ArrayList集合
泛型是一个未知的数据类型,当我们不确定为什么什么数据类型的时候,可以使用泛型
泛型可以接收任意的数据类型,可以使用Integer,String,Student...
创建对象的时候确定泛型的数据类型
 */
public class GenericClass<E> {

	private E name;

	public E getName() {
		return name;
	}

	public void setName(E name) {
		this.name = name;
	}
	
	
}

定义和使用含有泛型的方法(1)

package demo18;

/*
定义含有泛型的方法:泛型定义在方法的修饰符和返回值类型之间

格式:
修饰符 <泛型> 返回值类型 方法名(参数列表(使用泛型)){
	方法体;
}

含有发泛型的方法,在调用方法的时候确定泛型的数据类型
传递什么类型的参数,泛型就是什么类型

 */
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 demo18;

/*
测试含有泛型的方法
 */
public class Demo03GenericMethod {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		//创建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);
		
	}

}

定义和使用含有泛型的接口
在这里插入图片描述

package demo18;

/*
定义含有泛型的接口
 */
public interface GenericInterface<I > {
	public abstract void method(I i);
}

package demo18;

/*
	含有泛型的接口,第一种使用方式:定义接口的实现类,实现接口,指定接口的泛型
	public interface Iterator<E>{
		E.next();
	}
	
	Scanner类实现了Iterator接口,并指定接口的泛型为String,所以重写的next方法泛型默认就是String
	public final class Scanner implements Iterator<String>{
		public String next(){
		
		}
	}
	
 */
public class GenericInterfaceImpl1 implements GenericInterface<String>{

	@Override
	public void method(String s) {
		// TODO Auto-generated constructor stub
		System.out.println(s);
	}
}

package demo18;

/*
测试含有泛型的接口
 */
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);
	}
}

package demo18;

/*
含有泛型的接口第二种使用方式:接口使用什么泛型,实现类就使用什么泛型,类跟着接口走
就相当于定义了一个含有泛型的类,创建对象的时候确定泛型的类型
public interface List<E>{
	boolean add(E e);
	E get(int index);
}

public class ArraysList<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 demo18;

import java.util.ArrayList;
import java.util.Iterator;

/*
	泛型的通配符:
		?:代表任意的数据类型
	使用方式:
		不能创建对象使用
		只能作为方法的参数使用
		

 */
public class Demo05Generic {
	public static void main(String[] args) {
		ArrayList<Integer> list01 = new ArrayList<>();
		list01.add(1);
		list01.add(2);
		
		ArrayList<String> list02 = new ArrayList<>();
		list02.add("a");
		list02.add("b");
		
		printArray(list01);
		printArray(list02);
	}
	
	/*
	 定义一个方法,能遍历所有类型的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); 
		}
	}
}

package demo18;

import java.util.ArrayList;
import java.util.Collection;

/*
泛型的上限限定:?extends E 代表使用的泛型只能是E类型的子类/本身
泛型的下限限定:?super E 代表使用的泛型只能是E类型的子类/本身

 */
public class Demo06Generic {
	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);
		
		/*
		类与类之间的继承关系
		Integer extends Number extends Object
		String extends Object
		 */
	}

	//泛型的上限:此时的泛型?,必须是Number类型或者Number类型的子类
	public static void getElement1(Collection<? extends Number> coll) {}
	
	//泛型的下限:此时的泛型?,必须是Number类型或者Number类型的父类
	public static void getElement2(Collection<? super Number> coll) {}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值