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

  • Functional Kotlin
  • Mario Arias Rivu Chakraborty
  • 156字
  • 2021-06-24 19:15:21

Classes

Classes are the foundational type in Kotlin. In Kotlin, a class is a template that provides a state, a behavior, and a type to instances (more on that later).

To define a class, only a name is required:

class VeryBasic

VeryBasic is not very useful, but is still a valid Kotlin syntax.

The VeryBasic class doesn't have any state or behavior; nonetheless, you can declare values of type VeryBasic, as shown in the following code:

fun main(args: Array<String>) {
val basic: VeryBasic = VeryBasic()
}

As you can see, the basic value has a VeryBasic type. To express it differently, basic is an instance of VeryBasic.

In Kotlin, types can be inferred; so, the previous example is equivalent to the following code:

fun main(args: Array<String>) {
val basic = VeryBasic()
}

By being a VeryBasic instance, basic has a copy of the VeryBasic type's state and behavior, namely, none. So sad.

主站蜘蛛池模板: 晋江市| 甘肃省| 尤溪县| 玉山县| 奈曼旗| 客服| 温泉县| 雷州市| 宜宾市| 牡丹江市| 任丘市| 昭觉县| 临武县| 南皮县| 临湘市| 集安市| 西峡县| 霍城县| 温泉县| 太谷县| 瓦房店市| 通海县| 德令哈市| 保靖县| 方城县| 凤冈县| 石泉县| 页游| 图们市| 越西县| 泸州市| 波密县| 旬阳县| 钦州市| 汶川县| 丰城市| 阳春市| 扬中市| 南宫市| 普兰店市| 疏附县|