当前位置:网站首页>Chapter 12 generics
Chapter 12 generics
2022-07-27 12:14:00 【Haha's tea】
Catalog
2. Summary of using generics in Collections
3. How to customize generic structure : Generic classes 、 Generic interface ; Generic methods
1. About custom generic classes 、 Generic interface
Editor II 、 The embodiment of generics in inheritance
3、 ... and 、 The embodiment of generics in generics
(2) The use of restricted wildcards
5、 ... and 、 Examples of generic applications : Generic nesting
One 、 The use of generics
1.jdk 5.0 New features
Design the type of the element as a parameter , This type parameter is called generics .Collecton<E>,List<E>,ArrayList<E>, This <E> That's the type parameter , That is, generics .
The significance of generics :① Solve the security problem of element storage .② Solve the problem of type cast when getting data elements .


2. Summary of using generics in Collections
① Set interface or collection class in jdk5.0 It is modified to a generic structure .
② When instantiating a collection class , Specific generic types can be specified
③ After that, I will , In a collection class or interface, whenever a class or interface is defined , internal structure ( such as : Method 、 Constructors 、 Properties, etc ) Where to use generics of classes , Are specified as instantiated generic types . Such as :add(E e) ---> After instantiation :add(Integer e)
④ Be careful : The type of a generic must be a class , Cannot be a basic data type . Where basic data types are needed , Replace with packing .
⑤ If instantiated , The type of the generic is not specified . The default type is java.lang.Object type .
3. How to customize generic structure : Generic classes 、 Generic interface ; Generic methods


1. About custom generic classes 、 Generic interface
If a generic class is customized , Instantiation does not indicate the generic type of the class , The generic type is considered to be Object type
requirement : If a class is defined to be generic , It is suggested to specify the generic type of the class when instantiating .
References of different generics cannot be assigned to each other . When a generic method is called , Indicates the type of the generic parameter .
The internal structure of the class can use the generics of the class Class generics cannot be used in static methods Exception classes cannot be declared as generic classes
2. Generic methods
There is a generic structure in the method , Generic parameters have nothing to do with the generic parameters of the class . let me put it another way , It doesn't matter whether the generic method belongs to a generic class or not . Generic methods can be declared static . reason : Generic parameters are determined when the method is called , Not determined when instantiating a class .
Two 、 The embodiment of generics in inheritance


3、 ... and 、 The embodiment of generics in generics
Although the class A It's a class B Parent class of , but G<A> and G<B> They don't have the relationship of children and parents , The two are juxtaposed .
Add : class A It's a class B Parent class of ,A<G> yes B<G> Parent class of
Four 、 The use of wildcards
(1) wildcard
? class A It's a class B Parent class of ,G<A> and G<B> It doesn't matter. , The common parent of the two is : G<?>


(2) The use of restricted wildcards
? extends A: G<? extends A> It can be used as G<A> and G<B> Parent class of , among B yes A Subclasses of
? super A: G<? super A> It can be used as G<A> and G<B> Parent class of , among B yes A Parent class of

5、 ... and 、 Examples of generic applications : Generic nesting

边栏推荐
- Shell脚本文本三剑客之awk
- Newton Raphson iterative method
- Keil MDK compilation appears..\user\stm32f10x H (428): error: # 67: expected a "}" wrong solution
- Iptables firewall
- 广东财政多举措助力稳住粮食安全“压舱石”
- Unity shader - Laser special effect shader[easy to understand]
- Sync.map of go language
- kazoo使用教程
- Docker MySQL Usage Note
- Difference between verification and calibration
猜你喜欢

NPM step pit

Beyond compare 3 next difference segment / down search arrow not found
Ali II: what if the AOF file in redis is too large?

哈希表 详细讲解

Shake quickly to rescue the "frustrated person"

图像分割 vs Adobephotoshop(PS)

Shell script text three swordsmen sed

Shell脚本文本三剑客之awk

In the first half of the year, the number of fires decreased by 27.7%. Guangdong will improve the fire safety quality of the whole people in this way

Introduction to box diagram
随机推荐
【机器学习-白板推导系列】学习笔记---条件随机场
Mysql8msi installation tutorial (database mysql installation tutorial)
go入门篇 (4)
求不同采样周期下的传递函数有限零点
go 用本地代码replace
Solution: the idea project does not display a tree view
[网摘][医学影像] 常用的DICOM缩略图解释以及Viewer converter 转换工具
系统临时文件的写和读:createTempFile和tempFileContent[通俗易懂]
基于bolt数据库实现简单的区块链 day(2)
The bank's face recognition system was broken: a depositor was stolen 430000 yuan
象棋机器人「弄折了」棋童的手指。。。
While loop instance in shell
Detailed explanation of MATLAB S-function
微信小程序必用接口「建议收藏」
Weibo comment crawler + visualization
严控室外作业时间!佛山住建局发文:加强高温期间建筑施工安全管理
Firewall firewall
Could not load dynamic library ‘libcudnn.so.8‘;
[machine learning whiteboard derivation series] learning notes --- conditional random fields
Docker Mysql的使用note