Java泛型详解
Java泛型详解
一、泛型简介1、简介①泛型是一种未知的数据类型;(不知道用什么数据类型的时候,用泛型)②泛型也可以看做是一个变量,用来接收数据类型;2、使用泛型的好处没有使用泛型的好处:集合不使用泛型,默认是Object类型。可以存储任意类型的数据;没有使用泛型的弊端:集合不使用泛型,是不安全的,容易引发异常;使用泛型的好处:①避免了类型转换的麻烦,存取的类型是一致的;②将代码运行期的异常提升到
Java泛型详解
1、简介
①泛型是一种未知的数据类型;(不知道用什么数据类型的时候,用泛型)
②泛型也可以看做是一个变量,用来接收数据类型;
2、使用泛型的好处
没有使用泛型的好处:
集合不使用泛型,默认是Object类型。可以存储任意类型的数据;
没有使用泛型的弊端:
集合不使用泛型,是不安全的,容易引发异常;
使用泛型的好处:
①避免了类型转换的麻烦,存取的类型是一致的;
②将代码运行期的异常提升到了编译器上;
使用泛型的弊端:
泛型是什么类型就只能存储什么类型;
1、定义和使用一个含有泛型的类
还有泛型的学生类:
代码语言:javascript代码运行次数:0运行复制public class Student<E> {
private E name;
public Student() {
}
public Student(E name) {
= name;
}
public E getame() {
return name;
}
public void setame(E name) {
= name;
}
}
Test测试类:
代码语言:javascript代码运行次数:0运行复制public class Test {
public static void main(String[] args) {
//用整数
Student<Integer> student = new Student<>();
student.setame(100);
println(student.getame());//100
//用字符串
Student<String> student1 = new Student<>();
student1.setame("小明");
println(student1.getame());//小明
}
}
2、定义和使用一个含有泛型的方法
格式:
代码语言:javascript代码运行次数:0运行复制修饰符 <泛型> 返回值类型 方法名(参数列表(这个地方传递泛型参数)){
方法体;//返回值类型也可以是泛型,跟传入的泛型数据类型一致
}
使用:
学生类:
代码语言:javascript代码运行次数:0运行复制public class Student{
public <E> E get(E e){
return e;
}
}
Test测试类:
代码语言:javascript代码运行次数:0运行复制public class Test {
public static void main(String[] args) {
//用整数
Student student1 = new Student();
int i = student1.get(100);//100
println(i);
//用字符串
Student student = new Student();
String string = student.get("100");
println(string);//100
}
}
、定义和使用一个含有泛型的接口
接口:
代码语言:javascript代码运行次数:0运行复制public interface genericInterface<E> {
public abstract void get(E e);
}
实现类1(实现接口的时候确定数据类型):
代码语言:javascript代码运行次数:0运行复制public class GenericInterfaceImpl implements genericInterface<String> {
@Override
public void get(String s) {
println(s);
}
}
实现类2(实现接口的时候不确定数据类型,那么实现类也要用泛型):
代码语言:javascript代码运行次数:0运行复制public class GenericInterfaceImpl1<E> implements genericInterface<E> {
@Override
public void get(E e) {
println(e);
}
}
Test测试类:
代码语言:javascript代码运行次数:0运行复制public class Test {
public static void main(String[] args) {
//实现类1
GenericInterfaceImpl genericInterface = new GenericInterfaceImpl();
genericInterface.get("必须是字符串");//必须是字符串
//实现类2-字符串
GenericInterfaceImpl1<String> genericInterfaceImpl1 = new GenericInterfaceImpl1<>();
genericInterfaceImpl1.get("字符串");//字符串
//实现类2-数字
GenericInterfaceImpl1<Integer> genericInterfaceImpl2 = new GenericInterfaceImpl1<>();
genericInterfaceImpl2.get(100);//100
}
}
4、泛型通配符
泛型通配符:
?:代表任意的数据类型;
使用方式:
不能创建对象使用;
只能作为方法的参数使用;
使用:
代码语言:javascript代码运行次数:0运行复制package study.generic;
import java.util.ArrayList;
import java.util.Iterator;
public class Test {
public static void main(String[] args) {
//字符串-定义的时候不能用泛型通配符?
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("小明");
arrayList.add("小李");
arrayList.add("小兰");
printArray(arrayList);//小明 小李 小兰
//整数-定义的时候不能用泛型通配符?
ArrayList<Integer> arrayList1 = new ArrayList<>();
arrayList1.add(100);
arrayList1.add(200);
arrayList1.add(00);
printArray(arrayList1);//100 200 00
}
public static void printArray(ArrayList<?> arrayList){
for(Iterator<?> iterator = arrayList.iterator();iterator.hat();){
println(());
}
}
}
高级使用----受限泛型:(不常用,知道就行)
之前设置泛型的时候,实际上是可以任意设置的,只要是类就可以设置。但是在JAVA的泛型中可以指定一个泛型的上限和下限。
泛型的上限:
- 格式:
类型名称 <? extends 类 > 对象名称
- 意义:
只能接收该类型及其子类
泛型的下限:
- 格式:
类型名称 <? super 类 > 对象名称
- 意义:
只能接收该类型及其父类型
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
上传时间: 2025-07-23 19:22:11
上一篇:多分组的PCA图和top基因热图
下一篇:【Java设计模式】015
推荐阅读
留言与评论(共有 15 条评论) |
本站网友 backgroundworker | 24分钟前 发表 |
定义和使用一个含有泛型的类还有泛型的学生类:代码语言:javascript代码运行次数:0运行复制public class Student<E> { private E name; public Student() { } public Student(E name) { = name; } public E getame() { return name; } public void setame(E name) { = name; } }Test测试类:代码语言:javascript代码运行次数:0运行复制public class Test { public static void main(String[] args) { //用整数 Student<Integer> student = new Student<>(); student.setame(100); println(student.getame());//100 //用字符串 Student<String> student1 = new Student<>(); student1.setame("小明"); println(student1.getame());//小明 } }2 | |
本站网友 北京写字楼招租 | 19分钟前 发表 |
使用1 | |
本站网友 陆毅夫妇 | 2分钟前 发表 |
简介①泛型是一种未知的数据类型;(不知道用什么数据类型的时候 | |
本站网友 coco奶茶 | 21分钟前 发表 |
是不安全的 | |
本站网友 zhongtong | 1分钟前 发表 |
定义和使用一个含有泛型的类还有泛型的学生类:代码语言:javascript代码运行次数:0运行复制public class Student<E> { private E name; public Student() { } public Student(E name) { = name; } public E getame() { return name; } public void setame(E name) { = name; } }Test测试类:代码语言:javascript代码运行次数:0运行复制public class Test { public static void main(String[] args) { //用整数 Student<Integer> student = new Student<>(); student.setame(100); println(student.getame());//100 //用字符串 Student<String> student1 = new Student<>(); student1.setame("小明"); println(student1.getame());//小明 } }2 | |
本站网友 世界杯赛制 | 10分钟前 发表 |
使用1 | |
本站网友 发美100 | 28分钟前 发表 |
简介①泛型是一种未知的数据类型;(不知道用什么数据类型的时候 | |
本站网友 修眉毛 | 16分钟前 发表 |
可以存储任意类型的数据;没有使用泛型的弊端:集合不使用泛型 | |
本站网友 青岛电影 | 26分钟前 发表 |
只要是类就可以设置 | |
本站网友 马茂元 | 22分钟前 发表 |
简介①泛型是一种未知的数据类型;(不知道用什么数据类型的时候 | |
本站网友 免费企业邮箱注册 | 21分钟前 发表 |
Java泛型详解 一 | |
本站网友 洛阳二手房 | 28分钟前 发表 |
默认是Object类型 | |
本站网友 西方龙 | 30分钟前 发表 |
如有侵权请联系 cloudcommunity@tencent 删除前往查看泛型接口数据类型字符串java | |
本站网友 河北亿能烟塔工程有限公司 | 1秒前 发表 |
原始发表:2025-01-06 |