官术网_书友最值得收藏!

Generics

This section is just a short introduction to generics; later, we'll cover it in detail.

Generic programming is a style programming that focuses on creating algorithms (and collaterally, data structures) that work on general problems.

The Kotlin way to support generic programming is using type parameters. In a few words, we wrote our code with type parameters and, later on, we pass those types as parameters when we use them. 

Let's take, for example, our Oven interface:

interface Oven {
fun process(product: Bakeable)
}

An oven is a machine, so we could generalize it more:

interface Machine<T> {
fun process(product: T)
}

The Machine<T> interface defines a type parameter T and a method process(T).

Now, we can extend it with Oven:

interface Oven: Machine<Bakeable>

Now, Oven is extending Machine with the Bakeable type parameter, so the process method now takes Bakeable as a parameter.

主站蜘蛛池模板: 清涧县| 儋州市| 凉城县| 安多县| 湟中县| 黎城县| 墨脱县| 五华县| 迭部县| 出国| 东平县| 香格里拉县| 富蕴县| 尼玛县| 福安市| 邢台市| 隆子县| 嘉鱼县| 大宁县| 张家界市| 富裕县| 海原县| 五莲县| 青海省| 肃宁县| 保靖县| 承德县| 周宁县| 磴口县| 莱西市| 安龙县| 保康县| 肃宁县| 香港 | 阜新| 珠海市| 拜城县| 迭部县| 昌平区| 竹北市| 陈巴尔虎旗|