由于添加元素的集合的不确定,我可以添加任意类型的元素,那么在遍历的时候可能会出现问题。
我们之前学习过的数组,就可以声明一种类型,使得在添加数据时,数据类型不匹配的情况下,就会出现异常。
那么集合中有没有这样的方法呢?
有的,泛型。
泛型的概述
泛型是一种特殊的类型,可以理解为参数化类型,也就是可以把类型像参数一样传递。
泛型的格式:
<泛型类型>
泛型的用处
一般多用于集合。
泛型的好处
1、提高了程序的安全性,
2、将原本运行时期的错误在编译时期就暴露了出来。
3、省去了强制类型转换的麻烦。
package org.wdzl.unit07;
import java.util.ArrayList;
import java.util.Iterator;
public class StudentDemo {
public static void main(String[] args) {
ArrayList<Student> arrayList = new ArrayList<Student>();
Student student1 = new Student("张三",18);
Student student2 = new Student("刘备",22);
Student student3 = new Student("关羽",19);
arrayList.add(student1);
arrayList.add(student2);
arrayList.add(student3);
Iterator<Student> iterator = arrayList.iterator();
while (iterator.hasNext()){
Student student = iterator.next();
System.out.println(student.getName()+"------"+student.getAge());
}
}
}
泛型的应用
泛型类
把泛型定义在类上
泛型类型必须是引用类型。
package org.wdzl.unit07;
/**
* 泛型类:
* 概述:把泛型定义在类上。
* 格式:public class 类名<泛型类型1,……> 这里的泛型类型可以有多个
* 注意:泛型类必须时引用类型,而且要符合我们的命名规范。
*/
public class FanXIng<T> {
private T obj;
public T getObj() {
return obj;
}
public void setObj(T obj) {
this.obj = obj;
}
}
package org.wdzl.unit07;
public class FxDemo {
public static void main(String[] args) {
FanXIng<String> fanXIng = new FanXIng<String>();
fanXIng.setObj("郭富城");
String name = fanXIng.getObj();
System.out.println(name);
}
}
泛型方法
把泛型定义在方法上
public <泛型类型> 返回值类型 方法名(泛型类型)
package org.wdzl.unit07;
/**
* 泛型方法
* 概述:将泛型定义在方法上。
* 格式:public <泛型类型> 返回类型 方法名(泛型类型)
*/
public class Fx1 {
public <T> void show(T t){
System.out.println(t);
}
}
package org.wdzl.unit07;
public class Fx1Demo {
public static void main(String[] args) {
Fx1 fx1 = new Fx1();
fx1.show("hello");
fx1.show(100);
fx1.show(true);
}
}
泛型接口
把泛型定义在接口上
public interface 接口名<泛型类型>
package org.wdzl.unit07;
/**
* 泛型接口:
* 概述:把泛型定义在接口上。
* 格式:
* public interface 接口名<泛型类型>
*/
public interface Fx2<T> {
public abstract void show(T t);
}
package org.wdzl.unit07;
/**
* 在使用泛型接口时,需要明确到底什么时候明确泛型类型?
* 1、在实现类的时候就已经明确了泛型类型
* 2、使用的时候才知道泛型类型到底是什么。
*/
//方式1
//public class Fx2InterImpl implements Fx2<String>{
//
// @Override
// public void show(String o) {
// System.out.println(o);
// }
//}
//方式2
public class Fx2InterImpl<T> implements Fx2<T>{
@Override
public void show(T t) {
System.out.println(t);
}
}
package org.wdzl.unit07;
public class Fx2Demo {
public static void main(String[] args) {
// Fx2<String> fx2 = new Fx2InterImpl();
// fx2.show("java");
Fx2InterImpl<String> fx2Inter = new Fx2InterImpl<>();
fx2Inter.show("Java");
Fx2InterImpl<Integer> fx2Inter1 = new Fx2InterImpl<>();
fx2Inter1.show(2);
}
}
泛型的高级应用
泛型的高级应用:
1、当我们声明了一个具体的泛型类型时,我们传递的泛型只能是具体的。
2、泛型的通配符<?>
任意类型的泛型都是可以的。
3、<? extends E>
向上限定,只能是E及其子类。
4、<? super E>
向下限定,只能是E及其父类
package org.wdzl.unit07.superfx;
import java.util.ArrayList;
import java.util.Collection;
/**
* 泛型高级应用
*/
public class AnimalDemo {
public static void main(String[] args) {
// Collection<?> c1 = new ArrayList<Animal>();
// Collection<?> c2 = new ArrayList<Object>();
// Collection<?> c3 = new ArrayList<Animal>();
// Collection<?> c4 = new ArrayList<Cat>();
// Collection<? extends Animal> c1 = new ArrayList<Animal>();
// Collection<? extends Animal> c2 = new ArrayList<Object>();
// Collection<? extends Animal> c3 = new ArrayList<Animal>();
// Collection<? extends Animal> c4 = new ArrayList<Cat>();
// Collection<? super Animal> c1 = new ArrayList<Animal>();
// Collection<? super Animal> c2 = new ArrayList<Object>();
// Collection<? super Animal> c3 = new ArrayList<Dog>();
// Collection<? super Animal> c4 = new ArrayList<Cat>();
}
}