class 내에서 상수를 사용하는 방법
형식
class ClassName {
conpanion object {
const val 이름 = 값
}
}
[code]
fun main() {
val foodCourt = FoodCourt()
foodCourt.searchPrice(FoodCourt.FOOD_CREAM_PASTA)
foodCourt.searchPrice(FoodCourt.FOOD_STEAK)
foodCourt.searchPrice(FoodCourt.FOOD_PIZZA)
}
class FoodCourt {
fun searchPrice(foodName: String) {
val price = when(foodName) {
FOOD_CREAM_PASTA -> 13000
FOOD_STEAK -> 25000
FOOD_PIZZA -> 15000
else -> 0
}
println(“${foodName}의 가격은 ${price}입니다”)
}
companion object {
const val FOOD_CREAM_PASTA = “크림파스타”
const val FOOD_STEAK = “스테이크”
const val FOOD_PIZZA = “피자”
}
}
[/code]