1.简介
JDK5.0以后用泛型来接收类需要操作的引用数据类型,当引用的类型不确定时就使用泛型。
泛型技术是给编译器使用的,编译器在检查完后,生成的.class文件中不带泛型,原因是泛型是jdk5.0以后出现的技术,类加载器不能识别,这叫做擦除。在运行过程中,又带有泛型,原因是类加载器获取.class字节码文件中运行时类中的数据类型,进行类强制转换,这叫做泛型补偿。
2.泛型类
public class Person {
private String name;
private int 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;
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
public class Student extends Person {
public Student(String name, int age) {
super(name, age);
}
public void show(){
System.out.println("student show run...");
}
}
public class Worker extends Person {
public Worker(String name, int age) {
super(name, age);
}
public void show(){
System.out.println("worker show run...");
}
}
public class Tool<T>{
private T t;
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
}
public class TestClass {
public static void main(String[] args) {
Tool<Student> t = new Tool<Student>();
t.setT(new Student("steven",20));
//编译不通过,类型不匹配
//t.setT(new Worker("steven",20));
t.getT().show();
}
}
3.泛型方法
public class Person {
private String name;
private int 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;
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
public class Student extends Person {
public Student(String name, int age) {
super(name, age);
}
public void show(){
System.out.println("student show run...");
}
@Override
public String toString() {
return "name:"+getName()+",age:"+getAge();
}
}
public class Worker extends Person {
public Worker(String name, int age) {
super(name, age);
}
public void show(){
System.out.println("worker show run...");
}
@Override
public String toString() {
return "name:"+getName()+",age:"+getAge();
}
}
public class Tool<T>{
private T t;
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
public void display(T t){
System.out.println(t);
}
}
public class TestClass {
public static void main(String[] args) {
Tool<Student> t = new Tool<Student>();
t.display(new Student("steven",20));
//编译不通过,类型不匹配
//t.display(new Worker("steven",20));
}
}
4.泛型接口
interface Inter<I>{
void fun();
}
class InterClass implements Inter{
@Override
public void fun() {
System.out.println("InterClass fun run...");
}
}
public class TestClass {
public static void main(String[] args) {
InterClass i = new InterClass();
i.fun();
}
}
5.通配符
public static void main(String[] args) {
List<Student> list = new LinkedList<Student>();
list.add(new Student("steven",27));
list.add(new Student("cherry",20));
fun(list);
//这里存入的不相同,为了简化代码,不覆盖hashCode()与equals()
Set<Worker> set = new HashSet<Worker>();
set.add(new Worker("jan",23));
set.add(new Worker("tom",24));
fun(set);
}
private static void fun(Collection<?> coll) {
Iterator<?> it = coll.iterator();
while(it.hasNext()){
Person p = (Person)it.next();
System.out.println(p);
}
}
6.上限(类及其子类)
public static void main(String[] args) {
List<Student> list = new LinkedList<Student>();
list.add(new Student("steven",27));
list.add(new Student("cherry",20));
fun(list);
//这里存入的不相同,为了简化代码,不覆盖hashCode()与equals()
Set<Worker> set = new HashSet<Worker>();
set.add(new Worker("jan",23));
set.add(new Worker("tom",24));
fun(set);
}
private static void fun(Collection<? extends Person> coll) {
Iterator<?> it = coll.iterator();
while(it.hasNext()){
Person p = (Person)it.next();
System.out.println("name:"+p.getName()+",age:"+p.getAge());
}
}
7.下限(类及其父类)
public static void main(String[] args) {
List<Student> list = new LinkedList<Student>();
list.add(new Student("steven",27));
list.add(new Student("cherry",20));
fun(list);
Set<Person> set = new HashSet<Person>();
set.add(new Person("jan",23));
set.add(new Person("tom",24));
fun(set);
//这里存入的不相同,为了简化代码,不覆盖hashCode()与equals()
Set<Worker> set1 = new HashSet<Worker>();
set1.add(new Worker("jan",23));
set1.add(new Worker("tom",24));
//fun(set1);
//Student的下限不包含Worker,因此不可以打印
}
private static void fun(Collection<? super Student> coll) {
// TODO Auto-generated method stub
Iterator<?> it = coll.iterator();
while(it.hasNext()){
//取出的话得按下限来取,否则有的没法打印
Person p = (Person)it.next();
System.out.println("name:"+p.getName()+",age:"+p.getAge());
}
}