黑马程序员_泛型

------- android培训java培训、期待与您交流! --------

问题引出
class   GenericDemo
{
	public static void main(String[] args) 
	{
		
		ArrayList al=new ArrayList();
		al.add("abde01");
		al.add("abde02");//String 类型对象
		al.add(4);//Integer 类型对象
		Iterator it=al.iterator();
		while (it.hasNext())
		{
			String s=(String ) it.next();
			System.out.println(s+"..."s.length());

		}
	}
}
以上代码在编译时,不会报错,但是运行时会发生ClassCastException ,原因是集合中存储的String类型元素和Integer 类型元素,当迭代器获取String类型元素的时Integer不能转换成String ,我们希望在编译时期就能出现这个问题,让程序员发现以便解决,如果将存储的元素类型都存成同一种类型就不会出现该问题,那么就用到泛型。
泛型
泛型是JDK1.5以后出现的新特性,用于解决安全问题,是一个安全类型机制。

泛型的格式:通过<>确定引用数据类型
ArrayList <String> al=new  ArrayList<String>();//定义ArrayList集合,集合中的元素类型是String 

泛型的好处
1,将运行时期的问题ClassCastException转移到了编译时期,方便程序员解决问题,让运行时问题减少,提高安全性。
2,避免了强制转换的麻烦
Iterator<String> it=al.iterator()//迭代器上加上泛型,指定迭代器迭代的元素类型

什么时候使用泛型:
通常在集合框架中比较常见,通常见到<>就是泛型,<>用来接收数据类型
使用集合时,将集合中要存储的数据类型(引用数据类型)作为参数传入<>中
集合、迭代器、比较器都可以加泛型

泛型类
当类中要操作的引用数据类型不确定时,早期用Object 完成扩展,现在用泛型即可完成扩展
class Worker
{
}
class Student
{
}
class Utils<EE>//泛型类
{
	private EE e; 
	public void setObject(EE e)
	{
		this.e=e;
	}
	public EE getObject()
	{
		return e;
	}
}

class  FanxingDemo
{
	public static void main(String[] args) 
	{
		Utils<Worker> u=new Utils<Worker>();
		u.setObject(new Worker());//传入对象要创建对象
		Worker w=u.getObject();
	}
}
泛型方法
泛型类定义的泛型在整个类中有效,泛型类对象明确类型后,所要操作的类型就确定了,为了让不同的方法操作不同类型且类型不确定,则可将泛型定义在方法上。
class Demo
{
	public  <PP> void show(PP p)//泛型要加在返回类型前
	{
		System.out.println(p);
	}
	public  <QQ> void display(QQ q)//泛型定义在方法上
	{
		System.out.println(q);
	
	}
	
}
静态方法不可以访问类上定义的泛型,如果静态方法的数据类型不确定,可将泛型定义在方法上
//静态类型方法 泛型只能定义在方法上 不可以访问类上定义的泛型
	public static <T> void method(T t)
	{
		System.out.println(t);
	}
泛型接口
泛型定义在接口上
interface Inter<T>//定义泛型接口
{ 
	void show(T t);

}
class Interimp<T> implements Inter<T>//在实现泛型接口时不指定数据类型
{ 
	public void show(T t)
	{
		System.out.println(t);
	}
}
class  FanxingDemo3
{
	public static void main(String[] args) 
	{
		Interimp<String> i=new Interimp<String>();//在创建对象时由调用者指定数据类型
		i.show("hahahaha");
	}
}
interface Inter<T>//定义泛型接口
{ 
	void show(T t);

}
class Interimp<String> implements Inter<String>//在实现接口时指定数据类型
{ 
	public void show(String s)
	{
		System.out.println(s);
	}
}
class  FanxingDemo3
{
	public static void main(String[] args) 
	{
		Interimp i=new Interimp();//直接初始化对象
		i.show("hahahaha");//方法中传入String类型参数
	}
}

通配符“?”
当要传入的引用类型不确定时,可以用通配符(也叫占位符)"?"来占位,表示要传入的数据类型
import java.util.*;
class  GenernicDemo
{
	public static void main(String[] args) 
	{
		
		ArrayList<String> al=new ArrayList<String>();//String类型的集合
		al.add("abde01");
		al.add("abde02");
		al.add("abde03");
		al.add("abde04");

		ArrayList<Integer> al1=new ArrayList<Integer>();//Integer类型的集合
		al1.add(3);
		al1.add(6);
		al1.add(7);
		al1.add(10);
		printColl(al);
		printColl(al1);
	}
	public static void printColl(ArrayList<?> al)//两个集合都要使用迭代器读取集合中的元素,调用该方法,因此该方法传入的数据类型不确定,使用?占位符
	{
		Iterator<?> it=al.iterator();
		while (it.hasNext())
		{
			sop(it.next());
		}
	}
		public static void sop(Object obj)
	{
		System.out.println(obj);
	}

}
泛型限定
泛型限定是泛型的高级应用
上限限定: <? extend 父类E> 可接受E类型及其子类类型
下限限定:<? super 子类E>可接受该类型及其父类类型
import java.util.*;
class Person//定义Person类
{
	private String name;
	Person(String name)
	{
		this.name=name;
	}
	public String getName()
	{
		return name;
	}
}
class Student extends Person//Student 类继承Person 类
{
	Student(String name )
	{
		super(name);
	}
}
class   GenericDemo
{
	public static void main(String[] args) 
	{
		
		ArrayList<Person> al=new ArrayList<Person>();//定义集合,元素类型为Person
		al.add(new Person("abde01"));
		al.add(new Person("abde02"));
		al.add(new Person("abde03"));
		al.add(new Person("abde04"));
		//sop(al);
		ArrayList<Student> al1=new ArrayList<Student>();//定义集合,元素类型为Student	
		al1.add(new Student("stu04"));
		al1.add(new Student("stu03"));
		al1.add(new Student("stu02"));
		al1.add(new Student("stu01"));
		printColl(al1);
	}
上限限定
	public static void printColl(ArrayList<? extends Person> al)//上限限定,可以接收Person 类和Person 的子类
	{
		Iterator<? extends Person> it=al.iterator();
		while (it.hasNext())
		{
			sop(it.next().getName());
		}
	}
		public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}
下限限定
public static void printColl(ArrayList<? super Student> al)//下限限定,可以接收Student类和Student的父类
	{
		Iterator<? extends Person> it=al.iterator();
		while (it.hasNext())
		{
			sop(it.next().getName());
		}
	}

------- android培训java培训、期待与您交流! --------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值