【我的Java笔记】泛型

1.泛型的引出:

在集合中,当储存的元素类型和最后接受的类型不一致时,就会出现:“ClassCastException类转换异常”


2.泛型的概念:把数据类型的明确工作提供提前到了创建对象或者是调用方法的时期明确的一种特殊类型:参数化类型,可以像参数一样进行传递


3.格式:<引用类型>

:泛型只能存放引用类型!


4.泛型的优点:

(1)将运行时期异常提前到了编译时期

(2)获取数据的时候,不用强制类型转换了

(3)泛型可以提供程序的安全性


5.应用范围:泛型可以应用在接口,类,方法和集合中



泛型定义在集合

例1:

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

public class GenericDemo {
	
	public static void main(String[] args) {
		
		//创建一个ArrayList集合对象
		ArrayList<String> array = new ArrayList<String>() ;
		
		//给集合中添加元素
		array.add("hello") ;
		array.add("world") ;
		array.add("java") ;
		
		//获取迭代器对象并遍历
		Iterator<String> it = array.iterator() ;
		//遍历
		while(it.hasNext()){
			//不用强制类型转换了
			String s = it.next() ;
			System.out.println(s);
		}
		
	}
}




例2:泛型在集合自定义对象时的应用

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

public class GenericDemo {
	
	public static void main(String[] args) {
		//创建一个ArrayList集合对象
		ArrayList<Student> array = new ArrayList<Student>() ;
		
		//创建学生对象
		Student s1 = new Student("王昭君", 20) ;
		Student s2 = new Student("貂蝉", 25) ;
		Student s3 = new Student("杨贵妃", 29) ;
		Student s4 = new Student("凤姐", 22) ;
		
		//添加元素
		array.add(s1) ;
		array.add(s2) ;
		array.add(s3) ;
		array.add(s4) ;
		
		//获取迭代器
		Iterator<Student> it = array.iterator() ;
		while(it.hasNext()){
			Student s = it.next() ;
			System.out.println(s.getName()+"---"+s.getAge());
		}
	}
}



class Student {
	
	private String name ;
	private int age ;
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}

}








泛型定义在类上

1.将泛型定义在类上,解决对象在向下转型过程中出现的错误
2.注:<T>:表示任意类型

例:

public class ObjectDemo {

	public static void main(String[] args) {

		// 创建ObjectTool<T>对象
		ObjectTool<String> ot = new ObjectTool<String>();
		// 设置数据
		ot.setObj("高圆圆");
		// 获取数据
		// 编译都通过不了
		// Integer i = ot.getObj() ; 由于给类上加入了泛型,在实际测试中,给定了数据类型,获取数据的时候就必须应该类型来接收,否则不匹配
		String name = ot.getObj();
		System.out.println("姓名是:" + name);

		// 创建对象
		ObjectTool<Integer> ot2 = new ObjectTool<Integer>();
		// 设置数据
		ot2.setObj(27);
		// 获取数据
		Integer i = ot2.getObj();
		System.out.println("年龄是:" + i);
	}

}

// 工具类
class ObjectTool<T> {

	private T obj;

	// 获取
	public T getObj() {
		return obj;
	}

	// 设置
	public void setObj(T obj) {
		this.obj = obj;
	}
}











泛型定义在成员方法中

例:
public class ObjectDemo {

	public static void main(String[] args) {
		
		//创建工具类对象
		ObjectTool2 ot = new ObjectTool2() ;
		ot.show("hello") ;
		ot.show(true) ;
		ot.show(100) ;
	}
}

// 工具类
class ObjectTool2{
	
	//将泛型定义在方法上
	public <T> void show(T t){
		System.out.println(t);
	}
}










泛型定义在接口上

1.情况一:接口的子实现类已经知道传递的是什么数据类型
public class InterImpl<String> implements Inter<String> {

@Override
public void show(String t) {
System.out.println(t);
}
}

2.情况二:接口的子实现类在实现接口的时候,不知道具体的数据类型是什么
注:在测试类时,传入具体数据类型
public class InterImpl<T> implements Inter<T>{

@Override
public void show(T t) {
System.out.println(t);
}
}



例:
//测试类
public class InterfaceDemo {

	public static void main(String[] args) {
		
		Inter<Integer> i1 = new InterImpl<Integer>() ;
		i1.show(27) ;
		
		Inter<String> i2 = new InterImpl<String>() ;
		i2.show("高圆圆") ;
	}
}


//接口
interface Inter<T> {

	public abstract void show(T t) ;
}

//接口子实现类
class InterImpl<T> implements Inter<T>{

	public void show(T t) {
		System.out.println(t);
	}
}











泛型的高级通配符

1. <?> : 可以是任意类型,包括Object类型以及任意的Java类
2. <? extends E>: 向下限定,E类型以及E类型的子类

3. <? super E>: 向上限定,E类型以及E类型的父类


例:

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

public class GenericDemo {

	public static void main(String[] args) {

		// 创建Collection集合的对象
		// 前后的数据类型保持一致
		// Collection<Object> c1 = new ArrayList<Animal>() ;
		// Collection<Object> c2 = new ArrayList<Cat>() ;
		// Collection<Object> c3 = new ArrayList<Dog>() ; 错误:前后类型需一致
		Collection<Object> c3 = new ArrayList<Object>();

		// <?> :可以是任意类型,包括Object类型以及任意的Java类
		Collection<?> c4 = new ArrayList<Object>();
		Collection<?> c5 = new ArrayList<Animal>();
		Collection<?> c6 = new ArrayList<Cat>();
		Collection<?> c7 = new ArrayList<Dog>();

		// <? extends E>:向下限定,E类型以及E类型的子类
		Collection<? extends Object> c8 = new ArrayList<Object>();
		Collection<? extends Animal> c9 = new ArrayList<Animal>();
		// Collection<? extends Animal> c10 = new ArrayList<Object>() ; 错误:不能向上限定
		Collection<? extends Object> c11 = new ArrayList<Cat>();

		// <? super E>:向上限定,E类型以及E类型的父类
		Collection<? super Animal> c12 = new ArrayList<Object>();
		Collection<? super Animal> c13 = new ArrayList<Animal>();
		// Collection<? super Animal> c14 = new ArrayList<Dog>() ; 错误:不能向下限定
		Collection<? super Cat> c15 = new ArrayList<Animal>();
	}
}

// 自定义两个类
class Animal {

}

class Cat extends Animal {

}

class Dog extends Animal {

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值