초보자를 위한 Kotlin 200제에 나와 있는 예제 중 실습한 내용을 남겨 놓는다.
t108.kt
fun returnFunc(num: Int): () -> Unit = { println(num)}
fun main(args:Array<String>) {
val f:() -> Unit = returnFunc(30)
f()
}
t110.kt
fun <T> toFunction(value: T): () -> T = { value }
fun main(args:Array<String>) {
val func: () -> Int = toFunction<Int>(1107)
println(func())
}
t115.kt
interface ValueContainer {
fun getValue(): Int
}
class AAA : ValueContainer {
override fun getValue(): Int = 1102
}
class BBB : ValueContainer {
override fun getValue(): Int = 127
}
fun <T: ValueContainer> T.printValue() {
println(this.getValue())
}
fun main(args: Array<String>) {
AAA().printValue()
BBB().printValue()
}
t117.kt
fun main(args: Array<String>) {
val oneToTen: IntRange = 1..10
println(5 in oneToTen)
val upperAtoZ: CharRange = 'A'..'Z'
if('C' in upperAtoZ) {
println("대문자 입니다")
}
if('p' in 'a'..'z') {
println("소문자 입니다")
}
}
t121.kt
fun printAll(vararg tokens: String) {
for(token in tokens) {
print("$token ")
}
}
fun main(args: Array<String>) {
val numbers: Array<String> = arrayOf("What's", "your", "name?")
printAll(*numbers)
}
t135.kt
fun main(args: Array<String>) {
val list: Collection<Int> = listOf(10, 20, 10)
val set: Collection<Int> = setOf(1,2,3,2,3)
val map: Map<String, String> = mapOf("Apple" to "사과", "Banana" to "바나나")
println(list)
println(set)
println(map)
}