泛型

在不知道用什么数据类型的时候,可以使用泛型

public class ArrayList<E>
{
	public boolean add(E e){}
	public E get(int index){}
}

创建集合对象的时候,就会确定泛型的数据类型

ArrayList<String> list=new ArrayList<String>();

创建对象的时候,会把数据类型String作为参数传递

public class ArrayList<String>
{
	public boolean add(String e){}
	public String get(int index){}
}

创建集合对象的时候,就会确定泛型的数据类型

ArrayList<Student> list=new ArrayList<Student>();

创建对象的时候,会把数据类型String作为参数传递

public class ArrayList<Student>
{
	public boolean add(Student e){}
	public Student get(int index){}
}

泛型与不使用泛型

  • 集合不使用泛型,默认的类型就是Object类型,可以存储任意类型的数据,集合不是用泛型,类型存储的是Object类型,但可以转换成存储前的状态
  • 不使用泛型,不安全,会引发异常
  • 使用泛型存储的是什么类型,取出的就是什么类型,避免了类型转换的麻烦
  • 使用泛型,写代码的时候就能报错,而不使用泛型要运行以后才能看见错误
public class GenericDemo {
	public static void main(String[] args) {
	 	show01();
	 	show02();
	}
}

public static void show01(){
	ArrayList list=new ArrayList();
	##放进集合里的是Object类型
	list.add("abc");
	list.add(1);
	##使用迭代器遍历List集合
	##获取迭代器
	Iterator it=list.iterator();
	##使用迭代器的方法遍历迭代器
	##取出集合的是Object类型
	while(it.hasNext)
	{
	Object obj=it.next();
	##统计字符串长度
	##需要将Object类型向下转型,转变变成String类型
	String s=(String)obj;
	System.out.println(s.length);
	##会出现问题
	数字1,不能转换成String类型
}

public static void show02(){
	ArrayList<String> list=new ArrayList<>();
	list.add("ABC");
	##在写代码的时候,这句话会变红
	list.add(1);
	//使用迭代器遍历
	Iterator<String> it=list.iterator();
	while(it.hasNext())
	{
	String s=it.next();
	##可以直接使用
	System.out.println(s+"—>"+s.length());
}
}

定义泛型的类
模拟ArrayList

public class Genericlass
{
	private String name;
	public String getName(){
		return name;
	}

	public void setName(String name){
		this.name=name;
	}
}
public class GenericDemo {
	public static void main(String[] args) {
	 	Genericlass gc=new Genericlass();
	 	##因为在class里面定义了name是String类型
	 	gc.setName("只能是字符串");
	}
}

改进,把类改成是泛型

public class Genericlass<E>
{
	private E name;
	public E getName(){
		return name;
	}

	public void setName(E name){
		this.name=name;
	}
}
public class GenericDemo {
	public static void main(String[] args) {
	 	##不写泛型默认为Object类型
	 	Genericlass gc=new Genericlass();
	 	gc.setName("只能是字符串");
	 	Object obj=gc.getName();

	##使用Integer类型
		Genericlass<Integer> gc2=new Genericlass<>();
		gc2.setName(1);
		
	##使用String类型
		Genericlass<String> gc2=new Genericlass<>();
		gc2.setName("小明");
	}
}

定义含有泛型的方法
格式
修饰符<泛型形参> 返回值类型 方法名(参数列表(使用泛型)){
}

public class GenericMethod
{
	//定义一个含有泛型的方法
	public<M> void method1(M m){
	System.out.println(m);
	//定义一个含有泛型的静态方法
	public static <S> void method2(S s);
	}
}

使用定义的泛型
##调用的时候传递是什么,泛型就是什么

public class GenericDemo {
	public static void main(String[] args) {
	 //创建对象
	 GenericMethod gm=new GenericMethod();
	 ##传递的是Integer,调用的就是Integer
	 gm.method01(10);
	 gm.method01("abc");
	 gm.method01(8.8);
	 gm.method01(true);
	 ##静态方法直接使用类名去调用
	 GenericMethod.method2();
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值