当前位置:网站首页>什么是泛型?- 泛型入门篇
什么是泛型?- 泛型入门篇
2022-07-02 13:39:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
目录
1、什么是泛型?
泛型其实就是将类型作为参数传递,泛型允许程序员在编写代码时使用一些以后才指定的类型 ,在实例化该类时将想要的类型作为参数传递,来指明这些类型。
为什么要引入泛型?
例如:自己实现一个顺序表
public class MyArrayList {
public int[] elem;
public int usedSize;
public MyArrayList() {
this.elem = new int[10];
}
// add方法
public void add(int val) {
// 暂时不考虑扩容问题,这里只是为了讲泛型
elem[usedSize++] = val;
}
// get方法
public int get(int pos) {
return elem[pos];
}
}
这里可以看出,在使用上面这个自定义的顺序表时,我们只能添加 int 类型的元素,我们知道java集合中的顺序表,可以添加任何类型的数据,怎么实现的呢? 这里我们先尝试将 int 类型变为Object类型,这样就可以保证能传入任何类型。
public class MyArrayList {
public Object[] elem;
public int usedSize;
public MyArrayList() {
this.elem = new Object[10];
}
// add方法
public void add(Object val) {
// 暂时不考虑扩容问题,这里只是为了讲泛型
elem[usedSize++] = val;
}
// get方法
public Object get(int pos) {
return elem[pos];
}
}
main方法中往对象中添加数据时,可以添加任意类型的数据。但是,当需要取出数据时,因为返回的是Object类型,需要进行强转才能用相对应的类型来接收,非常麻烦。
public static void main(String[] args) {
MyArrayList myArrayList = new MyArrayList();
myArrayList.add(1);
myArrayList.add("hello");
int array1 = (int)myArrayList.get(0);
String array2 = (String)myArrayList.get(1);
}
所以问题来了,难道每次都要强转一下才能接收吗,能否不强转呢?这时候我们就想到在创建一个实例对象时,可以将想要的类型作为参数传递,让这个对象中全部存传入的类型的数据,那么拿出来的时候,就可以明确该对象中所有的数据都是该类型,不需要强转了。这就引入了泛型。
public class MyArrayList<E> {
// 在编写程序时,不指定具体的类型,而用<E>这里的E来暂时代替
// 具体的类型在实例化对象时传入
public E[] elem;
public int usedSize;
public MyArrayList() {
// 这里的写法不是特别准确,应该用反射机制,这里先这样写
this.elem = (E[])new Object[10];
}
// add方法
public void add(E val) {
// 暂时不考虑扩容问题,这里只是为了讲泛型
elem[usedSize++] = val;
}
// get方法
public E get(int pos) {
return elem[pos];
}
}
public static void main(String[] args) {
MyArrayList<Integer> myArrayList1 = new MyArrayList<>();
myArrayList1.add(1);
myArrayList1.add(3);
// myArrayList1.add("world");// 这里会报错,因为传入的数据不是指定的类型,所以泛型还有自动对类型进行检查的作用
int I1 = myArrayList1.get(0);
MyArrayList<String> myArrayList2 = new MyArrayList<>();
myArrayList2.add("hello");
myArrayList2.add("world");
String s1 = myArrayList2.get(0);
}
这样就保证了能传入任何类型的数据,同时在拿出时也不需要强转! 泛型的意义: 1、自动对类型进行检查 2、自动对类型进行强制类型转换
那么这里MyArrayList对应对象的类型是什么呢?是MyArrayList< Integer > 之类的吗?
这里可以看出,实例创建的对象他的类型都是MyArrayList,而<>中的内容不参与泛型类型的组成,那么<>里面的类型哪里去了呢?这就要了解泛型的工作机制了。
2、泛型是怎么编译的
数组和泛型之间的一个重要区别是它们如何强制类型检查。具体来说,数组在运行时存储和检查类型信息。但是,泛型在编译时检查类型错误,并且在运行时没有类型信息。
泛型的编译机制:擦除机制
在编译时,将 MyArrayList 中的 E 擦成了 Object 类型。
在main方法中都擦成了 MyArrayList 类型。
参考: 在java中创建泛型数组
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/147849.html原文链接:https://javaforall.cn
边栏推荐
- Library management system (Shandong Agricultural University Curriculum Design)
- 大厂面试总结大全
- Understand the key technology of AGV -- the difference between laser slam and visual slam
- OSPF - detailed explanation of NSSA area and full NSSA area (including configuration command), LSA type 7 lsa-7
- 618深度复盘:海尔智家的制胜方法论
- 618 reprise en profondeur: la méthode gagnante de la famille Haier Zhi
- Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation
- La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter
- Cloud native cicd framework: Tekton
- What if the win11 app store cannot load the page? Win11 store cannot load page
猜你喜欢
触发器:Mysql实现一张表添加或删除一条数据,另一张表同时添加
SQL solves the problem of continuous login deformation holiday filtering
What is the difference between self attention mechanism and fully connected graph convolution network (GCN)?
Seal Library - installation and introduction
Summary | three coordinate systems in machine vision and their relationships
Win11应用商店无法加载页面怎么办?Win11商店无法加载页面
隐私计算技术创新及产业实践研讨会:学习
Recalling the college entrance examination and becoming a programmer, do you regret it?
john爆破出现Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
Mysql database mysqldump why there is no statement to create a database
随机推荐
Unity uses ugui to set a simple multi-level horizontal drop-down menu (no code required)
ROW_NUMBER()、RANK()、DENSE_RANK区别
PWM controlled steering gear
Résumé de l'entrevue de Dachang Daquan
Classic quotations
The login box of unity hub becomes too narrow to log in
头条 | 亚控科技产品入选中纺联《纺织服装行业数字化转型解决方案重点推广名录》
Global and Chinese market of switching valves 2022-2028: Research Report on technology, participants, trends, market size and share
Bib | graph representation based on heterogeneous information network learning to predict drug disease association
Leetcode1380: lucky numbers in matrix
go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
618 deep resumption: Haier Zhijia's winning methodology
mysql数据库mysqldump为啥没有创建数据库的语句
Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation
Analysis of how to prevent virus in industrial computer
Yyds dry inventory method of deleting expired documents in batch
john爆破出现Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
Yyds dry goods inventory has not revealed the artifact? Valentine's Day is coming. Please send her a special gift~
PWM breathing lamp
Global and Chinese market of oil analyzers 2022-2028: Research Report on technology, participants, trends, market size and share