fun main() {
//本地函數(shù)
fun localFunc(){
println("Hello")
}
//對(duì)應(yīng)Java的local class
class LocalClass: Cloneable, Runnable{
override fun run() {}
}
}
public class JavaInnerClasses {
public static void main(String... args) {
class LocalClass implements Cloneable, Runnable {
@Override
public void run() { }
}
}
}
val book = Book(0, "Kotlin in Action", Person(1, "Dmitry", 40))
//編譯器生成的方法 copy component1
book.copy()
val id = book.component1()
val name = book.component2()
val author = book.component3()
enum class State {
Idle, Busy
}
//枚舉定義構(gòu)造函數(shù) 同java
enum class State1(val id: Int) {
Idle(0), Busy(1)
}
enum class Color {
White, Red, Green, Blue, Yellow, Black
}
fun main() {
State.Idle.name // Idle
State.Idle.ordinal // 0
val state = State.Idle
//枚舉全部值
val value = when (state) {
State.Idle -> { 0 }
State.Busy -> { 1 }
}
//枚舉創(chuàng)建區(qū)間
val colorRange = Color.White .. Color.Green
val color = Color.Blue //Blue不在區(qū)間內(nèi)
println(color in colorRange)
}
sealed class PlayerState
object Idle : PlayerState()
class Playing(val song: Song) : PlayerState() {
fun start() {}
fun stop() {}
}
class Error(val errorInfo: ErrorInfo) : PlayerState() {
fun recover() {}
}
完整的示例:控制播放器播放狀態(tài)的例子
data class Song(val name: String, val url: String, var position: Int)
data class ErrorInfo(val code: Int, val message: String)
object Songs {
val StarSky = Song("Star Sky", "https:///321144.mp3", 0)
}
sealed class PlayerState
object Idle : PlayerState()
class Playing(val song: Song) : PlayerState() {
fun start() {}
fun stop() {}
}
class Error(val errorInfo: ErrorInfo) : PlayerState() {
fun recover() {}
}
class Player {
var state: PlayerState = Idle
fun play(song: Song) {
this.state = when (val state = this.state) {
Idle -> {
Playing(song).also(Playing::start)
}
is Playing -> {
state.stop()
Playing(song).also(Playing::start)
}
is Error -> {
state.recover()
Playing(song).also(Playing::start)
}
}
}
}
fun main() {
val player = Player()
player.play(Songs.StarSky)
}
inline class State(val ordinal: Int) {
companion object {
val Idle = State(0)
val Busy = State(1)
}
fun values() = arrayOf(Idle, Busy)
val name: String
get() = when (this) {
State.Idle -> "Idle"
State.Busy -> "Busy"
else -> throw IllegalArgumentException()
}
}
inline class Color(val value: UInt) {
companion object {
val Red = Color(0xFFFF0000u)
val Green = Color(0xFF00FF00u)
val Blue = Color(0xFF0000FFu)
}
fun values() = arrayOf(Red, Green, Blue)
val name: String
get() = when (this) {
Red -> "Red"
Green -> "Green"
Blue -> "Blue"
else -> throw IllegalArgumentException()
}
}
fun main() {
State.Busy
Color.Blue
var boxInt = BoxInt(5)
if(boxInt < 10){
println("value is less than 10")
}
val newValue = boxInt.value * 200
println(newValue)
boxInt
println(boxInt)
}
密封類的實(shí)例:遞歸整型列表的簡(jiǎn)單實(shí)現(xiàn)
sealed class IntList {
object Nil: IntList() {
override fun toString(): String {
return "Nil"
}
}
data class Cons(val head: Int, val tail: IntList): IntList(){
override fun toString(): String {
return "$head, $tail"
}
}
fun joinToString(sep: Char = ','): String {
return when(this){
Nil -> "Nil"
is Cons -> {
"${head}$sep${tail.joinToString(sep)}"
}
}
}
}
fun IntList.sum(): Int {
return when(this){
IntList.Nil -> 0
is IntList.Cons -> head tail.sum()
}
}
operator fun IntList.component1(): Int? {
return when(this){
IntList.Nil -> null
is IntList.Cons -> head
}
}
operator fun IntList.component2(): Int? {
return when(this){
IntList.Nil -> null
is IntList.Cons -> tail.component1()
}
}
operator fun IntList.component3(): Int? {
return when(this){
IntList.Nil -> null
is IntList.Cons -> tail.component2()
}
}
fun intListOf(vararg ints: Int): IntList {
return when(ints.size){
0 -> IntList.Nil
else -> {
IntList.Cons(
ints[0],
//array前面加* 展開數(shù)組
intListOf(*(ints.slice(1 until ints.size).toIntArray()))
)
}
}
}
// [0, 1, 2, 3]
fun main() {
//val list = IntList.Cons(0, IntList.Cons(1, IntList.Cons(2, IntList.Cons(3, IntList.Nil))))
val list = intListOf(0, 1, 2, 3)
println(list)
println(list.joinToString('-'))
println(list.sum())
val (first, second, third) = list
println(first)
println(second)
println(third)
//val (a, b, c, d, e) = listOf<Int>()
}