当前位置:网站首页>什么是泛型?- 泛型入门篇
什么是泛型?- 泛型入门篇
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
边栏推荐
- 流批一体在京东的探索与实践
- Summary | three coordinate systems in machine vision and their relationships
- 618 deep resumption: Haier Zhijia's winning methodology
- Yyds dry inventory uses thread safe two-way linked list to realize simple LRU cache simulation
- 绝对真理和相对真理思考
- Seal Library - installation and introduction
- Yyds dry goods inventory has not revealed the artifact? Valentine's Day is coming. Please send her a special gift~
- 618深度复盘:海尔智家的制胜方法论
- 图书管理系统(山东农业大学课程设计)
- Global and Chinese market of oil analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

Yolov5 practice: teach object detection by hand

曆史上的今天:支付寶推出條碼支付;分時系統之父誕生;世界上第一支電視廣告...

Classifier visual interpretation stylex: Google, MIT, etc. have found the key attributes that affect image classification

TCP server communication process (important)

⌈ 2022 ⌋ how to use webp gracefully in projects
![OSPF - route aggregation [(summary) including configuration commands] | address summary calculation method - detailed explanation](/img/8b/36be3191a7d71f4a8c8181eaed8417.jpg)
OSPF - route aggregation [(summary) including configuration commands] | address summary calculation method - detailed explanation

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

头条 | 亚控科技产品入选中纺联《纺织服装行业数字化转型解决方案重点推广名录》

渗透工具-内网权限维持-Cobalt strike

Kubernetes family container housekeeper pod online Q & A?
随机推荐
LeetCode 2. 两数相加
基于Impala的高性能数仓实践之执行引擎模块
历史上的今天:支付宝推出条码支付;分时系统之父诞生;世界上第一支电视广告...
La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter
Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
LeetCode 1. 两数之和
图书管理系统(山东农业大学课程设计)
Day 18 of leetcode dynamic planning introduction
Where can I open computer administrator permissions
Summary of monthly report | list of major events of moonbeam in June
System Verilog实现优先级仲裁器
总结|机器视觉中三大坐标系及其相互关系
Global and Chinese market of discharge machines 2022-2028: Research Report on technology, participants, trends, market size and share
Mathematical analysis_ Notes_ Chapter 6: Riemann integral of univariate function
unity Hub 登录框变得很窄 无法登录
LeetCode 6. Z 字形变换 (N字形变换)
Download blender on Alibaba cloud image station
OSPF - route aggregation [(summary) including configuration commands] | address summary calculation method - detailed explanation
数据安全产业系列沙龙(三)| 数据安全产业标准体系建设主题沙龙
Unity Json 编写