当前位置:网站首页>泛型
泛型
2022-04-21 18:01:00 【RhineHe_hjs】
泛型
泛型:把类型明确的工作推迟到创建对象或调用方法的时候才去明确的特殊的类型
今天我们来聊聊集合中、类中和方法中以及参数中使用泛型
集合中的泛型
在list或者map中我们经常使用泛型,一旦我们确定好了使用的类型,那么集合就必须使用对应的类型,如果类型不一致就会报错。就拿ArrayList为例:
MyArrayList<Integer> list2 = new MyArrayList<>();
list2.add(10000);//参数类型必须和对象的泛型匹配
ArrayList<String> list3 = new ArrayList<>();
list3.add("懂了吗?");
方法中的泛型
我们知道很多时候方法都需要返回值的,但是对于有些方法我们不想固定死到底返回哪类数据类型的返回值,这时我们就可以返回泛型,比如线面的返回泛型的结合:
class A{
};
class B extends A{
}
//<?>:表示所有类型
public ArrayList<?> Method1() {
ArrayList<String> list =new ArrayList<>();
return list;
}
//<? extends A>:表示规定返回类型必须是A类或者其子类
private ArrayList<? extends A> Method2() {
ArrayList<A> list = new ArrayList<>();
ArrayList<B> list2 = new ArrayList<>();
return list;
}
//<? super B>:表示规定返回类型必须是B类或者其父类
private ArrayList<? super B> Method3() {
ArrayList<A> list = new ArrayList<>();
ArrayList<B> list2 = new ArrayList<>();
ArrayList<Object> list3 = new ArrayList<>();
return list;
}
可以看出上面的方法返回的结合泛型也分三类:
- ArrayList<?>:这个表示返回的数据类型是任意的,主要看方法体中返回什么集合类型;
- ArrayList<? extends A>:这个的返回值相对于第一种范围就比较窄一些,它的意思是要求返回的类型必须是A类或者其子类,这个子类呢不一定是直接子类
- <? super C>:这个呢和上一中表示刚好相反,要求返回值类型必须是C类或者是C的父类,这里的父类也可以不是直接父类,你甚至可以返回所有类的祖先类Object类。
类上的泛型
类名使用泛型,代表该类可以创建不同类型的对象,如以下类,这个类就可以创建多钟数据类型的对象
class MyArrayList<E>{
public Boolean add(E e) {
return true;
}
}
测试所写代码
import java.util.ArrayList;
import org.w3c.dom.ls.LSException;
public class GenericityTest {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(100);
//创建对象时必须规定对象的泛型类型
MyArrayList<Integer> list2 = new MyArrayList<>();
list2.add(10000);//参数类型必须和对象的泛型匹配
MyArrayList<String> list3 = new MyArrayList<>();
list3.add("懂了吗?");
}
class A{
};
class B extends A{
}
class C extends B{
}
//<?>:表示所有类型
public ArrayList<?> Method1() {
ArrayList<String> list =new ArrayList<>();
return list;
}
//<? extends A>:表示规定返回类型必须是A类或者其子类
private ArrayList<? extends A> Method2() {
ArrayList<A> list = new ArrayList<>();
ArrayList<B> list2 = new ArrayList<>();
ArrayList<C> list3 = new ArrayList<>();
return list3;
}
//<? super C>:表示规定返回类型必须是B类或者其父类
private ArrayList<? super C> Method3() {
ArrayList<A> list = new ArrayList<>();
ArrayList<B> list2 = new ArrayList<>();
ArrayList<Object> list3 = new ArrayList<>();
return list3;
}
}
//类名使用泛型,代表该类可以创建不同类型的对象
class MyArrayList<E>{
//方法的参数使用泛型,代表该函数可以接收不同类型的对象作为参数
public Boolean add(E e) {
return true;
}
}
参数中的泛型
由以上代码的方法中我们可以看到它的参数类型是E类型,这个类型是java自带的,它代表Element,元素的意思,使用这个类型作为参数列表时,你就可以传递各种引用类型的参数给该方法。
提示
以上只是我自己的看法,如有相同纯属意外。如有错误,也请谅解,勿喷!如有收获或疑问,欢迎点赞评论!
版权声明
本文为[RhineHe_hjs]所创,转载请带上原文链接,感谢
https://blog.csdn.net/yigan123/article/details/106676554
边栏推荐
- Shallow comparison between oceanbase and tidb - implementation plan
- [7:00 pm tonight] discussion on the development and application scenarios of metartc
- 【C语言重难点突破】——动态内存管理
- LogStash~LogStash的output(输出)
- What happens when the user sends a request to execute the controller method
- C# operator
- Akismet plugin tutorial WordPress prevent spam filtering plugin
- Shell编程学习(三)条件判断、流程控制
- 【今晚七点】metaRTC的发展和应用场景探讨
- Chest X-ray images - dataset
猜你喜欢
随机推荐
Ase35p03-asemi FET 35p03
Online examination of "financial law" in the computer examination of Northwest University of Technology
做自媒体副业真能月入上万?这篇分享,不藏私
Logstash ~ how logstash works
Vulnerability detection and defense: redis unauthorized access vulnerability recurrence
Part time jobs are higher than wages. In 2022, there are three sidelines with a monthly income of more than 10000
mysql查询表字段默认值
LogStash~LogStash的工作原理
"Industrial Internet plus safety production" to enhance the safety level of industrial enterprises
单片机能做什么,你有什么有单片机或开源硬件做的有意思的作品吗
Eating this open source gadget makes MCU development as efficient as Arduino
Headline we media operation secret script. If you stick to it, you can beat 90% of the people
Logstash ~ execution model of logstash
LogStash~LogStash的filter(过滤)
Akismet插件教程WordPress阻止过滤垃圾邮件插件
Chest X-ray images - dataset
修改van-dropdown-menu默认高度
Logstash ~ logstash one-stop tutorial (continuously updated)
Dynamic programming: coin topic summary
华为URPF


![[7:00 pm tonight] discussion on the development and application scenarios of metartc](/img/58/965e9bd295e34c4b37cd00aada2120.jpg)






