Daily Kotlin - 라벨 in lambda, break, continue, exception, 클래스, 상속(open)
- 라벨을 이용한 return
package chap04.section3.noinline
fun main() {
retFunc()
}
inline fun inlineLambda(a: Int, b: Int, out: (Int, Int) -> Unit) {
out(a, b)
}
fun retFunc() {
println("start of retFunc")
inlineLambda(13, 3) lit@{ a, b ->
val result = a + b
if(result > 10) return@lit
println("result: $result")
}
println("end of retFunc")
}
- 라벨을 이용한 break, continue
package chap04.section3
fun main() {
println("Break")
first@ for(i in 1..5) {
second@ for(j in 1..5) {
if(j == 3) break@first
println("i:$i, j:$j")
}
println("after for j")
}
println("after for i")
println("Continue")
first@ for(i in 1..5) {
second@ for(j in 1..5) {
if(j == 3) continue@first
println("i$i, j:$j")
}
println("after for j")
}
println("after for i")
}
Break
i:1, j:1
i:1, j:2
after for i
Continue
i1, j:1
i1, j:2
i2, j:1
i2, j:2
i3, j:1
i3, j:2
i4, j:1
i4, j:2
i5, j:1
i5, j:2
after for i
- try-catch
package chap04.section3
fun main() {
var a = 6
var b = 0
val c: Int
try {
c = a / b
} catch (e: Exception) {
println("Exception handled ${e.message}")
e.printStackTrace()
} finally {
println("finally executed")
}
}
Exception handled / by zero
finally executed
java.lang.ArithmeticException: / by zero
at chap04.section3.TryCatchKt.main(TryCatch.kt:9)
at chap04.section3.TryCatchKt.main(TryCatch.kt)
- Throw Exception
package chap04.section3
fun main() {
var amount = 600
try {
amount -= 100
checkAmount(amount)
} catch(e: Exception) {
println(e.message)
}
print("amount: $amount")
}
fun checkAmount(amount: Int) {
if(amount < 1000) {
throw Exception("잔고가 $amount 로 1000 이하 입니다.")
}
}
잔고가 500 로 1000 이하 입니다.
amount: 500
- Custom Exception
package chap04.section3
class InvalidNameException(message: String): Exception(message)
fun main() {
var name = "Kildong123"
try {
validateName(name)
} catch(e: Exception) {
println(e.message)
}
}
fun validateName(name: String) {
if(name.matches(Regex(".*\\d+.*"))) {
throw InvalidNameException("Your name : $name : contains numerals")
}
}
Your name : Kildong123 : contains numerals
- 부생성자
package chap05.section2.secondary
class Bird {
var name: String
var wing: Int
var beak: String
var color: String
constructor(name: String, wing: Int, beak: String, color: String) {
this.name = name
this.wing = wing
this.beak = beak
this.color = color
}
fun fly() = println("Fly wing: $wing")
fun sing(vol: Int) = println("Sing vol: $vol")
}
fun main() {
val coco = Bird("mybird", 2, "short", "blue")
coco.color = "yellow"
println("coco.color: ${coco.color}")
coco.fly()
coco.sing(3)
}
- 주생성자
package chap05.section2.primary
class Bird(var name: String, var wing: Int, var beak: String, var color: String) {
fun fly() = println("Fly wing: $wing")
fun sing(vol: Int) = println("Sing vol: $vol")
}
fun main() {
val coco = Bird("mybird", 2, "short", "blue")
coco.color = "yellow"
println("coco.color: ${coco.color}")
coco.fly()
coco.sing(3)
}
- 상속
open
키워드를 통해 상속 가능한 클래스임을 명시
package chap05.section3.openclass
open class Bird(var name: String, var wing: Int, var beak: String, var color: String) {
fun fly() = println("Fly wing: $wing")
fun sing(vol: Int) = println("Sing vol: $vol")
}
class Lark(name: String, wing: Int, beak: String, color: String): Bird(name, wing, beak, color) {
fun singHitone() = println("Happy Song!")
}
class Parrot: Bird {
val language: String
constructor(name: String, wing: Int, beak: String, color: String, language: String) : super(name, wing, beak, color) {
this.language = language;
}
fun speak() = println("Speak! $language")
}
fun main() {
val coco = Bird("mybird", 2, "short", "blue")
val lark = Lark("mylark", 2, "long", "brown")
val parrot = Parrot("myparrot", 2, "short", "multiple", "Korean")
lark.singHitone()
parrot.speak()
lark.fly()
parrot.sing(5)
print(coco.color)
}
Comments