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

  • Functional Kotlin
  • Mario Arias Rivu Chakraborty
  • 147字
  • 2021-06-24 19:15:22

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.

主站蜘蛛池模板: 晋中市| 上林县| 惠东县| 钟山县| 绥宁县| 兴安盟| 达孜县| 阜平县| 赤峰市| 晋中市| 西城区| 荆州市| 灵丘县| 东阿县| 将乐县| 新乡县| 延川县| 信宜市| 莲花县| 巍山| 太原市| 威信县| 精河县| 邹平县| 瑞昌市| 阿勒泰市| 马龙县| 金寨县| 铜梁县| 荔浦县| 昌江| 华容县| 玉环县| 屯门区| 九龙坡区| 龙口市| 灌阳县| 中西区| 深泽县| 图们市| 南投县|