2019年5月20日 星期一

Kotlin學習筆記(10) Classes

Kotlin學習筆記(10)  Classes

class Customer (var name: String)                    // 1

class Contact(val id: Int, var email: String)   // 2

fun main() {
    val customer = Customer("Alex")
    val contact = Contact(1, "mary@gmail.com")  // 4
println(customer.name)                         // 5
    println(contact.id)                         // 5
    println(contact.email)                         // 5   
}

Alex 1 mary@gmail.com

  1. Declares a class named Customer without any properties or user-defined constructors. A non-parameterized default constructor is created by Kotlin automatically.
  2. Declares a class with two properties: immutable id and mutable email, and a constructor with two parameters id and email.
  3. Creates an instance of the class Customer via the default constructor. Note that there is no new keyword in Kotlin.
  4. Creates an instance of the class Contact using the constructor with two arguments.
  5. Accesses the property id.
  6. Updates the value of the property email.

沒有留言:

張貼留言

RFID TI 培訓影片系列

RFID TI 培訓影片系列  https://www.ti.com/zh-tw/video/series/rfid.html 培訓影片系列 RFID 隨著創新技術日益發展,RFID 和 RF 術語越來越容易讓人混淆。本訓練系列詳細介紹了使用案例、權衡技術優缺點,讓您清楚知道該選...