当前位置:网站首页>什么是泛型?- 泛型入门篇
什么是泛型?- 泛型入门篇
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
边栏推荐
- Yolov5 practice: teach object detection by hand
- 数学分析_笔记_第5章:一元微分学
- Library management system (Shandong Agricultural University Curriculum Design)
- False summer vacation
- 618 reprise en profondeur: la méthode gagnante de la famille Haier Zhi
- TCP congestion control details | 2 background
- Where can I open computer administrator permissions
- Global and Chinese market of switching valves 2022-2028: Research Report on technology, participants, trends, market size and share
- mysql数据库mysqldump为啥没有创建数据库的语句
- Mathematical analysis_ Notes_ Chapter 6: Riemann integral of univariate function
猜你喜欢

Yyds dry inventory company stipulates that all interfaces use post requests. Why?

Typescript array out of order output

Practice of traffic recording and playback in vivo
![[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

Leetcode1380: lucky numbers in matrix

SQL solves the problem of continuous login deformation holiday filtering

mysql数据库mysqldump为啥没有创建数据库的语句

OSPF - detailed explanation of NSSA area and full NSSA area (including configuration command), LSA type 7 lsa-7

sim2real环境配置教程

PWM breathing lamp
随机推荐
Practice of traffic recording and playback in vivo
According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
Exploration and practice of integration of streaming and wholesale in jd.com
[fluent] dart data type string type (string definition | string splicing | string API call)
[fluent] dart data type list set type (define set | initialize | generic usage | add elements after initialization | set generation function | set traversal)
Trigger: MySQL implements adding or deleting a piece of data in one table and adding another table at the same time
云原生的 CICD 框架:Tekton
Multi task prompt learning: how to train a large language model?
Bone conduction non ear Bluetooth headset brand, bone conduction Bluetooth headset brand recommendation
头条 | 亚控科技产品入选中纺联《纺织服装行业数字化转型解决方案重点推广名录》
Global and Chinese markets for carbon dioxide laser cutting heads 2022-2028: Research Report on technology, participants, trends, market size and share
历史上的今天:支付宝推出条码支付;分时系统之父诞生;世界上第一支电视广告...
Cloud native cicd framework: Tekton
PWM breathing lamp
A week of short video platform 30W exposure, small magic push helps physical businesses turn losses into profits
虚假的暑假
[error record] the connection of the flutter device shows loading (disconnect | delete the shuttle/bin/cache/lockfile file)
ROW_ NUMBER()、RANK()、DENSE_ Rank difference
go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
Understand the key technology of AGV -- the difference between laser slam and visual slam