[Kotlin]學習筆記 - Types

繼續學習 Kotin,本篇筆記會紀錄 Types

這裡的 Types 就是常聽到的 Class 和 interface

筆記

Interface

  1. 預設皆為 public

  2. interface 有 basic definitions、default methods、properties

    1
    2
    3
    4
    interface Time {
    fun setTime(hours: Int, mins: Int = 0, secs: Int =0)
    fun setTime(time: KevinTime) = setTime(time.hours, time.mins, time.secs)
    }
  3. 要 implement 一個 interface 是使用 : 來表示 implements

    1
    class YetiTime: Time {}
  4. 實做一個 interface 的 method 要使用 override keyword

    1
    2
    3
    class YetiTime: Time {
    override fun setTime(hours: Int, mins: Int, secs: Int) {}
    }
  5. multiple implements

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    interface Time {
    fun setTime(hours: Int, mins: Int = 0, secs: Int = 0)
    fun setTime(time: KevinTime) = setTime(time.hours, time.mins, time.secs)
    }

    interface EndOfTheWorld {
    fun setTime(time: KevinTime) {}
    }

    class YetiTime : Time, EndOfTheWorld {
    override fun setTime(time: KevinTime) {
    super<Time>.setTime(time)
    }

    override fun setTime(hours: Int, mins: Int, secs: Int) {
    }
    }
    • 使用 , 來實現多 interface implements
    • 可使用 super<T>.xxx 來決定使用哪一個 interface 的 default methods

Classes

  1. finalpublic by default - apply to class and functions

  2. 使用 open keyword 讓 function 可以被 override,讓 Class 可以被繼承

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    open class Person {
    var firstName: String = ""
    var lastName: String = ""
    open fun getName(): String = "$firstName $lastName"
    }

    class Student: Person() {
    override fun getName(): String {
    return "Hello"
    }
    }
  3. with abstract keyword,該 class 或 function 就必須被 implement

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    fun main() {
    var a = Person() // 出現 Error: Cannot create an instance of an abstract class
    }

    abstract class Person {
    var firstName: String = ""
    var lastName: String = ""

    open fun getName(): String = "$firstName $lastName"
    abstract fun getAddress(): String
    }

    class Student: Person() {
    override fun getAddress(): String {
    return ""
    }

    override fun getName(): String {
    return "Hello"
    }

    }

  4. sealed Class: represent restricted class hierarchies that provide more control over inheritance

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    sealed class Operation {
    class Add(val value: Int) : Operation()
    class Subtract(val value: Int) : Operation()
    class Multiply(val value: Int) : Operation()
    class Divide(val value: Int) : Operation()
    }

    fun execute(x: Int, op: Operation) = when (op) {
    is Operation.Add -> x + op.value
    is Operation.Subtract -> x - op.value
    is Operation.Multiply -> x * op.value
    is Operation.Divide -> x / op.value
    }
    • 很像 Enum
    • 可以搭配 when 做 pattern match
  5. 可以透過 construction parameters 定義 class property (Primary Constructor)

    1
    2
    3
    open class Person(val name: String)
    val kevin = Person("Kevin")
    println(kevin.name)
  6. 可以設定第二組 constructor

    1
    2
    3
    4
    5
    open class Person(val name: String) {
    constructor(name: String, age: Int): this(name)
    }
    // 使用預設值
    open class Person(val name: String, var age: Int = 0)
  7. Data Classes: 會自動實做 equals, hashCode, toString methods,且又是 immutable class 再加上 copy method

1
2
3
4
5
6
7
8
9
data class Meeting(val name: String, val location:String)
val aMeeting = Meeting("A Meeting", "Taiwan")
val bMeeting = Meeting("A Meeting", "Taiwan")
val anotherMeeting = aMeeting.copy(location = "Japan")

// equal:
aMeeting == bMeeting // true
// ToString
println(aMeeting.ToString()) // Meeting(name=A Meeting, location=Taiwan)
  • equal: 會比對 data class 內得資料是否相同
  • ToString() 會印出文字而非 hashCode
  • 可以使用 copy 來建立另外一個 data class

Objects

  1. object keyword 用來建立 singleton

  2. 定義與建立發生在同一時間

  3. object 可以有 properties, methods, initializers 但就是不能有 constructors

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    class Course(val id: Int, val title: String)

    object Courses{
    var allCourses = arrayListOf<Course>()
    }

    fun enrole(courseName: String) {
    Courses.allCourses
    .filter { it.title == courseName }
    .firstOrNull()
    }
  4. object 可以在 class 內出現,而此時他的 scope 會是該 class 內

  5. companion object: Factory object and static members

    1
    2
    3
    4
    5
    6
    7
    8
    9
    class Person {
    companion object {
    fun createNormalPerson(name: String): NormalPerson {
    return NormalPerson(name)
    }
    }
    }

    val normalPerson = Person.createNormalPerson("XXX")
  6. @JvmStatic to mark as static method for Java compiler

參考資料