当前位置:网站首页>什么是泛型?- 泛型入门篇
什么是泛型?- 泛型入门篇
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
边栏推荐
- Kubernetes family container housekeeper pod online Q & A?
- Does bone conduction earphone have external sound? Advantages of bone conduction earphones
- Today in history: Alipay launched barcode payment; The father of time-sharing system was born; The first TV advertisement in the world
- Student course selection system (curriculum design of Shandong Agricultural University)
- 曆史上的今天:支付寶推出條碼支付;分時系統之父誕生;世界上第一支電視廣告...
- PCL 最小中值平方法拟合平面
- Foreign enterprise executives, continuous entrepreneurs, yoga and skiing masters, and a program life of continuous iteration and reconstruction
- LeetCode 3. 无重复字符的最长子串
- 电脑设备打印机驱动安装失败如何解决
- Mathematical analysis_ Notes_ Chapter 6: Riemann integral of univariate function
猜你喜欢

How to choose the right kubernetes storage plug-in? (09)
![[fluent] dart data type number type (DART file creation | num type | int type | double type | num related API)](/img/c7/1949894e106036d2b412bcd6f70245.jpg)
[fluent] dart data type number type (DART file creation | num type | int type | double type | num related API)

SSM integration exception handler and project exception handling scheme

流批一体在京东的探索与实践

Mysql database mysqldump why there is no statement to create a database

PyC file decompile

串口控制舵机转动
![[North Asia data recovery] data recovery case of raid crash caused by hard disk disconnection during data synchronization of hot spare disk of RAID5 disk array](/img/51/f9c1eed37794db8c8d0eefd60b9e3d.jpg)
[North Asia data recovery] data recovery case of raid crash caused by hard disk disconnection during data synchronization of hot spare disk of RAID5 disk array

Typescript array out of order output

Text intelligent expansion and contraction control of swiftui text component (tutorial includes source code)
随机推荐
PWM controlled steering gear
Today in history: Alipay launched barcode payment; The father of time-sharing system was born; The first TV advertisement in the world
隐私计算技术创新及产业实践研讨会:学习
Thinking about absolute truth and relative truth
Yyds dry inventory method of deleting expired documents in batch
大廠面試總結大全
LeetCode 4. 寻找两个正序数组的中位数(hard)
一文读懂AGV的关键技术——激光SLAM与视觉SLAM的区别
618 deep resumption: Haier Zhijia's winning methodology
False summer vacation
数据安全产业系列沙龙(三)| 数据安全产业标准体系建设主题沙龙
Student course selection system (curriculum design of Shandong Agricultural University)
总结|机器视觉中三大坐标系及其相互关系
How to use stustr function in Oracle view
Does bone conduction earphone have external sound? Advantages of bone conduction earphones
Yyds dry inventory KVM new inventory to expand space for home
PWM breathing lamp
LeetCode 3. Longest substring without duplicate characters
LeetCode 4. Find the median (hard) of two positive arrays
电脑设备打印机驱动安装失败如何解决