Content preview from Head First Kotlin程序设计

目录
xv
从输出推断输入
人人都喜欢通用的代码。
使用泛型是令代码一致且不易出错的一种策略
。
在本章中
,
我们将看到
Kotlin
的集合类
如何利用泛型避免将
Cabbage
对象放入
List<S
eagull>
。
你将了解编写泛型类
、
接口和函
数的场景和方法
,
以及如何将泛型类型限制为特定的父类型
。
最后
,
你将学习利用协
变和逆变控制泛型类型的行为
。
泛型
10
泛型在集合中的使用
290
如何定义
MutableList 291
在
MutableList
中使用类型参数
292
通过泛型类或接口可以做什么
293
接下来要做的事
294
创建
Pet
类的层次结构
295
定义
Contest
类
296
添加
scores
属性
297
创建
getW
inners
方法
298
创建一些
Contest
对象
299
创建
Generics
项目
301
Retailer
层次结构
305
定义
Retailer
接口
306
轻松创建
CatRetailer
、
DogRetailer
和
FishRetailer
等对象
307
用
out
使泛型类型协变
308
更新
Generics
项目
309
再定义一个
Ve
t
类
313
创建
Vet
对象
314
用
in
使泛型类型逆变
315
泛型类型的局部逆变
316
更新
Generics
项目
317
Kotlin
工具箱
324
使用泛型
,
对象只能作为
Duck
对象的引用进去
……
……
以
Duck
类型的引用出来
。
MutableList<Duck>
Duck Duck Duck Duck
Duck Duck Duck Duck
Vet<T: Pet>
treat(t: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access
More than 5,000 organizations count on O’Reilly
O’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.Julian F.
I wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.Addison B.
I’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.Amir M.
I'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.Mark W.
Publisher Resources
ISBN: 9787111662358