泛型
约 668 字大约 2 分钟
2026-04-04
泛型
创建集合时,如果不指定类型
存储的类型为Object,可用存储任意的值
不安全,可能发生类型转换异常
ArrayList list=new ArrayList();
list.add(1);
list.add("u");
for (Object o : list) {
Integer.parseInt((String) o);
}
创建集合时,
使用泛型
避免类型转换
可在编译期时检查异常
类型固定,只能存储对应类型的数据
泛型里的通配符
? 表示任意的数据类型
使用方式:
不能创建对象的时候使用,只能作为方法的参数
不能定义到类上
当我们不知道用什么类型来接收的时候,可使用
?,作为通配符,来表示未知的
public static void main(String[] args) {
Collection<String> coll=new ArrayList<>();
getElement(coll);
}
public static void getElement(Collection<?> coll){
}//错误,通配符 ? 不能定义到类上
class Person<?>{
void method(){
}
}受限泛型
可以指定泛型的上限和下限
泛型上限格式
类型名称<? extends 类>对象名称只能接收该类型和其子类
泛型下限格式
类型名称<? super 类>对象名称只能接收该类型 和其父类
//Human类
public class Human {
}
//Person类 继承于Human类
public abstract class Person extends Human {
abstract void method(Collection<? extends Person> coll);
abstract void methodA(Collection<? super Person> coll);
}
//Student类 继承于 Person
public class Student extends Person {
@Override
void method(Collection<? extends Person> coll) {
System.out.println("hello");
}
@Override
void methodA(Collection<? super Person> coll) {
System.out.println("world");
}
}
public static void main(String[] args) {
Person person=new Student();
Collection<Person> coll=new ArrayList();
Collection<Student> coll2=new ArrayList();
Collection<Human> coll3=new ArrayList();
person.method(coll);
person.method(coll2);
// person.method(coll3); Collection只能接受Person和子类
person.methodA(coll);
// person.methodA(coll2); 只能接受Person和父类
person.methodA(coll3);
}泛型定义类
在定义类 的时候使用E:表示任意类型
泛型类定义格式
// E
修饰符 class 类名称<泛型>{}为什么使用?
在创建对象时,不知道使用什么类型,需要定义一个泛型
public class DemoClass {
public static void main(String[] args) {
GenirecClass<String> gc=new GenirecClass<>("扎三");
}
}
class GenirecClass<E> {
private E name;
public E getName() {
return name;
}
public void setName(E name) {
this.name = name;
}
public GenirecClass(E name) {
this.name = name;
}
}泛型方法定义格式
// T
修饰符 <泛型类型> 返回值类型 方法名(参数){}接口泛型
在定义接口 的时候使用I:表示任意类型
泛型接口定义格式
// <I>
修饰符 interface 接口名称<泛型>{}public interface DemoInterface<I> {
void method(I name);
I method();
}
public class DemoInterfaceImpl implements DemoInterface<String>{
@Override
public void method(String name) {
}
}带有通配符的泛型
当使用泛型类 或者接口 ,传递数据时,泛型类型不确定 ,可以使用<?>表示
贡献者
更新日志
2026/4/5 03:39
查看所有更新日志
fb8bc-更新为vuepress于