繼續學習 Kotin,本篇筆記會紀錄 Types
這裡的 Types 就是常聽到的 Class 和 interface
筆記
Interface
-
預設皆為 public
-
interface 有 basic definitions、default methods、properties
1
2
3
4interface Time {
fun setTime(hours: Int, mins: Int = 0, secs: Int =0)
fun setTime(time: KevinTime) = setTime(time.hours, time.mins, time.secs)
} -
要 implement 一個 interface 是使用
:
來表示 implements1
class YetiTime: Time {}
-
實做一個 interface 的 method 要使用
override
keyword1
2
3class YetiTime: Time {
override fun setTime(hours: Int, mins: Int, secs: Int) {}
} -
multiple implements
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17interface 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
-
final
、public
by default - apply to class and functions -
使用
open
keyword 讓 function 可以被 override,讓 Class 可以被繼承1
2
3
4
5
6
7
8
9
10
11open class Person {
var firstName: String = ""
var lastName: String = ""
open fun getName(): String = "$firstName $lastName"
}
class Student: Person() {
override fun getName(): String {
return "Hello"
}
} -
with
abstract
keyword,該 class 或 function 就必須被 implement1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23fun 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"
}
} -
sealed Class: represent restricted class hierarchies that provide more control over inheritance
1
2
3
4
5
6
7
8
9
10
11
12
13sealed 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
-
可以透過
construction parameters
定義 class property (Primary Constructor)1
2
3open class Person(val name: String)
val kevin = Person("Kevin")
println(kevin.name) -
可以設定第二組 constructor
1
2
3
4
5open class Person(val name: String) {
constructor(name: String, age: Int): this(name)
}
// 使用預設值
open class Person(val name: String, var age: Int = 0) -
Data Classes: 會自動實做 equals, hashCode, toString methods,且又是 immutable class 再加上 copy method
1 | data class Meeting(val name: String, val location:String) |
- equal: 會比對 data class 內得資料是否相同
- ToString() 會印出文字而非 hashCode
- 可以使用 copy 來建立另外一個 data class
Objects
-
object
keyword 用來建立 singleton -
定義與建立發生在同一時間
-
object 可以有 properties, methods, initializers 但就是不能有 constructors
1
2
3
4
5
6
7
8
9
10
11class Course(val id: Int, val title: String)
object Courses{
var allCourses = arrayListOf<Course>()
}
fun enrole(courseName: String) {
Courses.allCourses
.filter { it.title == courseName }
.firstOrNull()
} -
object 可以在 class 內出現,而此時他的 scope 會是該 class 內
-
companion object: Factory object and static members
1
2
3
4
5
6
7
8
9class Person {
companion object {
fun createNormalPerson(name: String): NormalPerson {
return NormalPerson(name)
}
}
}
val normalPerson = Person.createNormalPerson("XXX") -
@JvmStatic
to mark as static method for Java compiler