泛型
泛型的概念
- 泛型是一种未知的数据类型,当我们不知道使用什么数据类型的时候,可以使用泛型
- 泛型也可以看出是一个变量,用来接收数据类型
E e
:Element
元素T t
:Type
类型
ArrayList
集合在定义的时候,不知道集合都会存储什么类型的数据,所以类型使用泛型E
:未知数据类型- 创建集合对象的时候,就会确定泛型的数据类型
使用泛型的优点
- 创建集合对象,使用泛型
- 好处:
- 避免了类型转换的麻烦,存储的是什么类型,取出的就是什么类型
- 把运行期异常(代码运行之后会抛出的异常),提升到了编译期(写代码的时候会报错)
- 弊端:
- 泛型是什么类型,只能存储什么类型的数据
- 创建集合对象,不使用泛型
- 好处:
- 集合不使用泛型,默认的类型就是Object类型,可以存储任意类型的数据
- 弊端:
- 不安全,会引发异常
演示
- 主类
package com.idayuanshuai.www.Demo2;
import javax.lang.model.element.Element;
import java.util.ArrayList;
import java.util.Iterator;
public class ElementClassDemo1 {
public static void main(String[] args) {
demo1();
demo2();
}
private static void demo2() {
ArrayList<String> list = new ArrayList<>();
list.add("WWW.ME");
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
private static void demo1() {
ArrayList list = new ArrayList();
list.add("123");
list.add(89);
Iterator iterator = list.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
System.out.println(list.size());
}
}
- 输出结果
123
89
2
WWW.ME
定义和使用含有泛型的类
- 定义一个含有泛型的类,模拟
ArrayList
集合 - 泛型是一个未知的数据类型,当我们不确定什么什么数据类型的时候,可以使用泛型
- 泛型可以接收任意的数据类型,可以使用
Integer,String,Student...
- 创建对象的时候确定泛型的数据类型
演示
- 其他类
package com.idayuanshuai.www.Demo2;
public class GenericClass<E> {
private E name;
public GenericClass(E name) {
this.name = name;
}
public GenericClass() {
}
public E getName() {
return name;
}
public void setName(E name) {
this.name = name;
}
}
- 主类
package com.idayuanshuai.www.Demo2;
public class GenericClassDemo2 {
public static void main(String[] args) {
demo1();
}
private static void demo1() {
GenericClass<String> list = new GenericClass<>("i am you");
System.out.println(list.getName());
GenericClass<Integer> list2 = new GenericClass<>();
list2.setName(123);
System.out.println(list2.getName());
GenericClass<Object> object = new GenericClass<>();
object.setName("123"+45+900L+34.12);
System.out.println(object.getName());
}
}
- 输出结果
i am you
123
1234590034.12
含有泛型的方法
- 定义含有泛型的方法:泛型定义在方法的修饰符和返回值类型之间
- 格式:
修饰符 <泛型> 返回值类型 方法名(参数列表(使用泛型)){
方法体;
}
- 含有泛型的方法,在调用方法的时候确定泛型的数据类型,传递什么类型的参数,泛型就是什么类型
演示一
- 其他类
package com.idayuanshuai.www.Demo2;
public class GenericMethod {
public <E> void method(E e){
System.out.println(e);
}
public static <E> void methodStatic(E e){
System.out.println(e);
}
}
- 主类
package com.idayuanshuai.www.Demo2;
public class GenericClassDemo3 {
public static void main(String[] args) {
demo1();
System.out.println();
System.out.println("========================");
System.out.println();
demo2();
}
private static void demo2() {
GenericMethod.methodStatic(78);
GenericMethod.methodStatic(56);
GenericMethod.methodStatic("123");
GenericMethod.methodStatic(23.12);
GenericMethod.methodStatic(true);
}
private static void demo1() {
GenericMethod genericMethod = new GenericMethod();
genericMethod.method("me");
genericMethod.method(123);
genericMethod.method(true);
genericMethod.method(12.345);
}
}
- 输出结果
me
123
true
12.345
========================
78
56
123
23.12
true
含有泛型的接口
定义格式:
修饰符 interface接口名<代表泛型的变量> { }
举例:
public interface MyGenericInterface<E>{
public abstract void add(E e);
public abstract E getE();
}
使用方法一
- 含有泛型的接口,第一种使用方式:定义接口的实现类,实现接口,指定接口的泛型
public interface Iterator<E> {
E next();
}
Scanner
类实现了Iterator
接口,并指定接口的泛型为String
,所以重写的next
方法泛型默认就是String
public final class Scanner implements Iterator<String>{
public String next() {}
}
方法二
- 含有泛型的接口第二种使用方式:接口使用什么泛型,实现类就使用什么泛型,类跟着接口走
就相当于定义了一个含有泛型的类,创建对象的时候确定泛型的类型
public interface List<E>{
boolean add(E e);
E get(int index);
}
public class ArrayList<E> implements List<E>{
public boolean add(E e) {}
public E get(int index) {}
}
演示
- 接口
package com.idayuanshuai.www.Demo3;
public interface GenericInterface<E> {
public abstract void method(E e);
}
- 实现类
package com.idayuanshuai.www.Demo3;
public class GenericInterfaceImpl implements GenericInterface<String>{
@Override
public void method(String s) {
System.out.println(s);
}
}
package com.idayuanshuai.www.Demo3;
public class GenericInterfaceImpl1<E> implements GenericInterface<E> {
@Override
public void method(E e) {
System.out.println(e);
}
}
- 主类
package com.idayuanshuai.www.Demo3;
public class GenericClass {
public static void main(String[] args) {
GenericInterfaceImpl genericInterface = new GenericInterfaceImpl();
genericInterface.method("whoami");
GenericInterface in = new GenericInterfaceImpl();
in.method("object");
GenericInterfaceImpl1<Integer> genericInterfaceImpl1 = new GenericInterfaceImpl1<>();
genericInterfaceImpl1.method(123);
GenericInterfaceImpl1<String> StringGenericInterfaceImpl1 = new GenericInterfaceImpl1<>();
StringGenericInterfaceImpl1.method("String method");
}
}
- 输出结果
whoami
object
123
String method
泛型通配符
当使用泛型类或者接口时,传递的数据中,泛型类型不确定,可以通过通配符<?>
表示。但是一旦使用泛型的通配符后,只能使用Object
类中的共性方法,集合中元素自身方法无法使用。
泛型的通配符:不知道使用什么类型来接收的时候,此时可以使用?,?表示未知通配符。
此时只能接受数据,不能往该集合中存储数据。
演示
- 主类
package com.idayuanshuai.www.Demo4;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class GenericDemo1 {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(123);
list.add(234);
list.add(89);
ArrayList<String> list1 = new ArrayList<>();
list1.add("whoami");
list1.add("idys");
method(list1);
method(list);
}
public static void method(ArrayList<?> list){
Iterator<?> iterator = list.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
- 输出结果
whoami
idys
123
234
89
通配符高级使用----受限泛型
之前设置泛型的时候,实际上是可以任意设置的,只要是类就可以设置。但是在JAVA的泛型中可以指定一个泛型的上限和下限。
泛型的上限:
- 格式: :类型名称 <? extends 类 > 对象名称
- 意义: 只能接收该类型及其子类
泛型的下限:
- 格式: 类型名称 <? super 类 > 对象名称
- 意义: 只能接收该类型及其父类型
- 类与类之间继承关系
Integer extends Number extends Object
String extends Object
演示
- 主类
package com.idayuanshuai.www.Demo4;
import java.util.ArrayList;
import java.util.Collection;
public class GenericDemo2 {
public static void main(String[] args) {
Collection<Integer> list1 = new ArrayList<Integer>();
Collection<String> list2 = new ArrayList<String>();
Collection<Number> list3 = new ArrayList<Number>();
Collection<Object> list4 = new ArrayList<Object>();
getElement(list1);
//getElement(list2); 报错
getElement(list3);
// getElement(list4); 报错
// getElement2(list1); 报错
// getElement2(list2); 报错
getElement2(list3);
getElement2(list4);
}
// 泛型的上限:此时的泛型?,必须是Number类型或者Number类型的子类
public static void getElement(Collection<? extends Number> coll){}
// 泛型的下限:此时的泛型?,必须是Number类型或者Number类型的父类
public static void getElement2(Collection<? super Number> coll){}
}