1.什么是泛型
泛型是一种安全机制。程序的安全问题,由原来的运行时期,提到了编译时期。
使用泛型的好处:避免了数据类型的强制转换
//==================================================
2.如何使用泛型
泛型的格式:
集合类<数据类型> 变量 = new 集合类<数据类型>();
指示这个集合,只能存储已经定义的数据类型
/*
* 泛型的安全性
*/
import java.util.*;
public class GenericDemo {
public static void main(String[] args) {
ArrayList<String> array = new ArrayList<String>();
array.add("afe");
array.add("btefd");
array.add("qewd");
array.add("befv");
//迭代的过程中,输出每一个字符串的长度
//存储的数据类型,不是String,导致出现类型转换异常,异常发生在运行时期的
Iterator<String>it = array.iterator();
while(it.hasNext()){
String s = it.next();
System.out.println(s.length());
}
}
}
//==================================================
3.自定义泛型
利用泛型,提高程序的安全性,不可以随便传递参数
/*
* 自定义的泛型
* 写一个工厂,创建对象
*/
class Student{}
class Worker{}
//为了解决运行时期的安全性问题,对这个工厂类,进行泛型的定义
// new Factory<Student>
class Factory<T>{
private T t;
public void setQ(T t){
this.t = t;
}
public QQ getQ(){
return t;
}
}
public class GenericDemo1 {
public static void main(String[] args) {
Factory<Student> f = new Factory<Student>();
f.setQ(new Student());
Student s = f.getQ();
System.out.println(s);
}
}
//==================================================
4.泛型的类,泛型的方法
泛型类,在定义类的时候,在类的右边写<变量>
泛型方法,是在方法的声明上写<变量>
/*
* 泛型类和泛型方法
*/
//xt变量,建立对象的时候,传递什么,变量就变成什么数据类型
class Generic<xt>{
public void print(xt s){
System.out.println(s);
}
//在方法的定义上写泛型,此时这个方法,不受类上泛型的约束
public <QQ> void show(QQ q){
System.out.println(q);
}
//静态方法中,定义泛型,泛型写在static后面
public static<T> void method(T t){
System.out.println(t);
}
}
public class GenericDemo2 {
public static void main(String[] args) {
Generic<Integer> g = new Generic<Integer>();
g.print(124);
g.show(12.34);
Generic.method(false);
}
}
//==================================================
5.泛型接口
在接口上定义泛型
接口是需要类实现
类实现接口,不指定泛型,由创建实现类的同时指定类型
类实现接口,直接指定泛型
/*
* 泛型接口
*/
interface MyInter<T>{
public abstract void show(T t);
}
//定义实现类,实现的时候,不指定泛型,由创建对象的时候,指定泛型
class MyIntertImpl<T> implements MyInter<T>{
public void show(T t){
System.out.println(t);
}
}
//定义实现类,直接指定泛型
class MuMyIntertImpl2 implements MyInter<Integer>{
public void show(Integer t){
System.out.println(t);
}
}
public class GenericDemo3 {
public static void main(String[] args) {
MyIntertImpl<String> my = new MyIntertImpl<String>();
my.show("abc");
MuMyIntertImpl2 my2 = new MuMyIntertImpl2();
my2.show(123);
}
}
//==================================================
6.泛型的通配符
泛型的通配符<?>表示不清楚泛型是什么数据类型
如果只简单的做遍历迭代是可以的,提高安全性,避免强制转换不可以了
import java.util.*;
public class GenericDemo4 {
public static void main(String[] args) {
ArrayList<String> array = new ArrayList<String>();
array.add("abc1");
array.add("abc2");
array.add("abc3");
method(array);
HashSet<Integer> hs = new HashSet<Integer>();
hs.add(1231);
hs.add(1232);
hs.add(1233);
hs.add(1234);
method(hs);
//迭代器迭起两个集合
//将迭代器提取成一个方法,当你需要的时候,直接调用方法
}
private static void method(Collection<?> coll){
Iterator<?> it = coll.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
//==================================================
7.泛型的限定
泛型限定的格式<? XXX Xx>, 泛型限定的使用,必须有继承和多态
泛型的上限格式:<? extends E> 传递E类型,和E的子类类型。泛型的上限(限制的是最高父类)
泛型的下限格式:<? super T>传递T类型,和T的父类类型,泛型的下限(限制最低的子类。
泛型接受本类类型,接受父类类型
//==================================================
自定义泛型案例:
public class GenericPerson implements Comparable<GenericPerson>{
private String name;
private int age;
public int compareTo(GenericPerson p){
int num = this.name.compareTo(p.name);
return num == 0 ? this.age - p.age : num ;
}
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;
}
public GenericPerson(String name, int age) {
this.name = name;
this.age = age;
}
}
///=====
/*
* HashSet 集合存储自定义对象Person,加上泛型
*/
import java.util.*;
public class HashSetDemo {
public static void main(String[] args) {
// HashSet<Person> hs = new HashSet<Person>();
TreeSet<GenericPerson> hs = new TreeSet<GenericPerson>(new MyComparator());
hs.add(new GenericPerson("haha",101));
hs.add(new GenericPerson("haha",10));
hs.add(new GenericPerson("xixi",10));
hs.add(new GenericPerson("haha",10));
hs.add(new GenericPerson("hehe",10));
Iterator<GenericPerson> it = hs.iterator();
while(it.hasNext()){
GenericPerson p = it.next();
System.out.println(p.getName() +"..." + p.getAge());
}
}
}
class MyComparator implements Comparator<GenericPerson>{
public int compare(GenericPerson p1, GenericPerson p2){
int num = p1.getAge() - p2.getAge();
return num == 0 ? p1.getName().compareTo(p2.getName()) : num;
}
}