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

  • 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.

主站蜘蛛池模板: 延长县| 邹城市| 合川市| 铁力市| 宁国市| 射洪县| 松阳县| 太湖县| 柳江县| 宣恩县| 大英县| 贺州市| 平南县| 罗平县| 尼木县| 阿坝县| 东乡县| 青州市| 乌拉特中旗| 易门县| 蕉岭县| 甘孜| 浦北县| 浑源县| 洪湖市| 南投市| 洞口县| 河南省| 顺平县| 织金县| 察隅县| 徐闻县| 任丘市| 合山市| 伊通| 潢川县| 出国| 二连浩特市| 沾益县| 北京市| 柘荣县|