当前位置:网站首页>什么是泛型?- 泛型入门篇
什么是泛型?- 泛型入门篇
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
边栏推荐
- PCL point cloud image transformation
- 总结|机器视觉中三大坐标系及其相互关系
- Student course selection system (curriculum design of Shandong Agricultural University)
- Data security industry series Salon (III) | data security industry standard system construction theme Salon
- Lampe respiratoire PWM
- LeetCode 2. 两数相加
- LeetCode 3. Longest substring without duplicate characters
- Yyds dry goods inventory has not revealed the artifact? Valentine's Day is coming. Please send her a special gift~
- PWM breathing lamp
- 流批一体在京东的探索与实践
猜你喜欢

How to solve the failure of printer driver installation of computer equipment

路由模式:hash和history模式

Yyds dry goods inventory hands-on teaching you to carry out the packaging and release of mofish Library (fishing Library)

A week of short video platform 30W exposure, small magic push helps physical businesses turn losses into profits

基于多元时间序列对高考预测分析案例

Hard core! One configuration center for 8 classes!

Multi task prompt learning: how to train a large language model?

Which software is good for machine vision?

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)
随机推荐
LeetCode 4. 寻找两个正序数组的中位数(hard)
False summer vacation
618 deep resumption: Haier Zhijia's winning methodology
Data security industry series Salon (III) | data security industry standard system construction theme Salon
触发器:Mysql实现一张表添加或删除一条数据,另一张表同时添加
[fluent] dart data type boolean type (boolean type definition | logical operation)
总结|机器视觉中三大坐标系及其相互关系
Yolov5 practice: teach object detection by hand
Kubernetes three open interfaces first sight
外企高管、连续创业者、瑜伽和滑雪高手,持续迭代重构的程序人生
Global and Chinese markets for airport baggage claim conveyors 2022-2028: technology, participants, trends, market size and share Research Report
图书管理系统(山东农业大学课程设计)
Routing mode: hash and history mode
Some problems about MySQL installation
Global and Chinese market of oil analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
Cloud native cicd framework: Tekton
Yyds dry goods inventory hands-on teaching you to carry out the packaging and release of mofish Library (fishing Library)
SSM整合-异常处理器及项目异常处理方案
Yyds dry goods inventory # look up at the sky | talk about the way and principle of capturing packets on the mobile terminal and how to prevent mitm
HMS core machine learning service helps zaful users to shop conveniently