-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClassesObjectKeyword.kt
More file actions
52 lines (45 loc) · 1013 Bytes
/
Copy pathClassesObjectKeyword.kt
File metadata and controls
52 lines (45 loc) · 1013 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package tutorial
fun main(args: Array<String>){
// Call static function of a class
println(OperationCenter.info())
// A singleton using 'object' keyword
NameList.names.add("John")
NameList.names.add("Barbara")
println(NameList.names)
// Anonymous class
val einsteinEq = object : Equation {
override fun display() {
println("E = M x C ^ 2")
}
}
einsteinEq.display()
}
class OperationCenter {
/**
* Use 'companion object {}' block to declare
* static variables and functions
*/
companion object {
val id = 12345 // static variable
/**
* @JvmStatic is used to let Java access
* the static function using OperationCenter.info()
*/
@JvmStatic
fun info(): String{
return "center id: $id, status: running"
}
}
}
/**
* A singleton class of names
*/
object NameList{
val names : MutableList<String> = mutableListOf()
}
/**
* An interface to be implemented in an anonymous class
*/
interface Equation{
fun display()
}