当前位置:网站首页>第12章 泛型
第12章 泛型
2022-07-27 10:46:00 【哈哈泡的茶】
目录
一、泛型的使用
1.jdk 5.0新增的特性
把元素的类型设计成一个参数,这个类型参数叫做泛型。Collecton<E>,List<E>,ArrayList<E>,这个<E>就是类型参数,即泛型。
泛型的意义:①解决元素存储的安全性问题。②解决获取数据元素时需要类型强制转换的问题。


2.在集合中使用泛型的总结
① 集合接口或集合类在jdk5.0时都修改为带泛型的结构。
② 在实例化集合类时,可以指明具体的泛型类型
③ 指明完以后,在集合类或接口中凡是定义类或接口时,内部结构(比如:方法、构造器、属性等)使用到类的泛型的位置,都指定为实例化的泛型类型。如:add(E e) ---> 实例化以后:add(Integer e)
④ 注意点:泛型的类型必须是类,不能是基本数据类型。需要用到基本数据类型的位置,拿包装类替换。
⑤ 如果实例化时,没有指明泛型的类型。默认类型为java.lang.Object类型。
3.如何自定义泛型结构:泛型类、泛型接口;泛型方法


1.关于自定义泛型类、泛型接口
如果自定义了泛型类,实例化没有指明类的泛型,则认为此泛型类型为Object类型
要求:如果定义了类是带泛型的,建议在实例化时要指明类的泛型。
泛型不同的引用不能相互赋值。 泛型方法在调用时,指明泛型参数的类型。
类的内部结构就可以使用类的泛型 静态方法中不能使用类的泛型 异常类不能声明为泛型类
2.泛型方法
在方法中出现了泛型的结构,泛型参数与类的泛型参数没有任何关系。换句话说,泛型方法所属的类是不是泛型类都没有关系。泛型方法可以声明为静态的。原因:泛型参数是在调用方法时确定的,并非实例化类时确定的。
二、泛型在继承上的体现


三、泛型在泛型方面的体现
虽然类A是类B的父类,但G<A>和G<B>二者不具备子父类关系,二者是并列关系。
补充:类A是类B的父类,A<G>是B<G>的父类
四、通配符的使用
(1)通配符
? 类A是类B的父类,G<A>和G<B>没有关系,二者共同的父类是: G<?>


(2)有限制条件的通配符的使用
? extends A: G<? extends A> 可以作为G<A>和G<B>的父类,其中B是A的子类
? super A: G<? super A> 可以作为G<A>和G<B>的父类,其中B是A的父类

五、泛型应用举例:泛型嵌套

边栏推荐
- Installation and use of GTEST and gmock
- 数字三角形模型 AcWing 1027. 方格取数
- Luogu p3052 [usaco12mar]cows in a skyscraper G
- Budweiser, a well-known beer, plans to launch NFT in an attempt to unveil the "long planned" uplink?
- Memory search acwing 901. Skiing
- Internal and external troubles of digital collection NFT "boring ape" bayc
- 背包问题 AcWing 9. 分组背包问题
- Description and feelings
- 8 find subsequences with a maximum length of K
- "My" bug collection (Reprinted)
猜你喜欢

Yonbuilder enables innovation, and the "golden keyboard Award" of the fourth UFIDA developer competition is open!

How to build a data index system is the most effective. From 0 to 1, we will take you a quick start - 02 live review

Data assets are king. How to analyze the relationship between enterprise digital transformation and data asset management?

Memory search acwing 901. Skiing

Wechat push - template message parameter configuration

求组合数 AcWing 888. 求组合数 IV

Asustek unparalleled, this may be the best affordable high brush thin notebook on the screen

Game theory acwing 891. Nim game

博弈论 AcWing 891. Nim游戏

Remember an experience of using canvas to make the banner streamer effect of Tencent cloud homepage
随机推荐
Vscode establishes automatic search of header files under non engineering directories
Take you hand-in-hand to develop a complete classic game [Tetris] from scratch, with less than 200 lines of logic.
数字三角形模型 AcWing 1015. 摘花生
Win10 vscode code code format setting and remote breakpoint debugging
The longest ascending subsequence model acwing 1016. The sum of the largest ascending subsequence
Digital triangle model acwing 1027. Grid retrieval
"My" bug collection (Reprinted)
最长上升子序列模型 AcWing 272. 最长公共上升子序列
Wenzhou University X kangaroo cloud: how to "know well" in the construction of higher talent education
IO stream_ Character stream, IO stream summary, IO stream case summary
ethereum rpc
Redis high availability principle
Longest ascending subsequence model acwing 272. longest common ascending subsequence
JVM judges that the object is dead, and practices verify GC recycling
Introduction to software vulnerability analysis (I)
SQL Server2000 database error
力扣——10. 正则表达式匹配
Luogu p1441 weight weighing
Basic use of cmake
Gaussian elimination acwing 883. solving linear equations with Gaussian elimination