#1 μ½λ - μ½νλ¦°
#1-1
fun main() {
var testInteger : Int = 7
testInteger = testInteger.plusAndMultiply(3, 5)
println("${testInteger}") // κ²°κ³Όλ‘ 50μ΄ μΆλ ₯λλ€.
}
fun Int.plusAndMultiply(plusValue : Int, multiplyValue : Int) : Int {
return (this + plusValue) * multiplyValue
}
μ΄ μ½λλ νμ₯ ν¨μ(Extension functions)λ₯Ό μ¬μ©νλ μμ΄λ€.β¨β¨νμ₯ ν¨μλ κΈ°μ‘΄ ν΄λμ€μ λ©€λ² ν¨μμΈ κ²μ²λΌ νΈμΆν μ μμ§λ§, ν΄λΉ ν΄λμ€μ μ§μ μ μλ λ©€λ²κ° μλ μΈλΆμμ μ μλ ν¨μλ€. νμ₯ ν¨μμ λ¬Έλ²μ μμ£Ό κ°λ¨νλ€. ν¨μ μ΄λ¦ μμ ν΄λμ€ λλ μΈν°νμ΄μ€ λͺ μ λΆμ΄κΈ°λ§ νλ©΄ λλ€.β¨μ¬λ΄μΌλ‘, μλ°μλ μ½νλ¦°μ νμ₯ ν¨μμ κ°μ κΈ°λ₯μ μμ§λ§ λΉμ·ν μν μ μννλ Default methodκ° μλ€.
#1-2
fun main() {
val hongGilDong = Person("νκΈΈλ", 175, 65, 3579)
Person.hello()
hongGilDong.selfIntroduction()
hongGilDong.introduceBMI()
Person.bye()
}
class Person (val name: String, val height: Int, val weight: Int, private val memberNumber : Int) {
companion object {
fun hello() {
println("Hello")
}
fun bye() {
println("GoodBye")
}
}
fun selfIntroduction() {
println("My name is ${name}. I am ${height}cm tall and weigh ${weight}kg")
}
}
fun Person.introduceBMI() {
val weightKG = this.weight.toDouble()
val heightM = this.height.toDouble() / 100
val BMI = weightKG / (heightM * heightM) // heightM * heightMμ κ΄νΈλ₯Ό μμ°μ§ μμΌλ©΄ BMI == weightKGκ° λμ΄λ²λ¦¬λ μ£Όμ
val roundedBMI = "%.1f".format(BMI)
println("My BMI is ${roundedBMI}")
}
/* β μλ¬κ° λ°μνλ μ½λ
fun Person.introduceMemberNumber() {
println("My memberNumber is ${this.memberNumber}")
}
*/
/* μΆλ ₯ κ²°κ³Ό
Hello
My name is νκΈΈλ. I am 175cm tall and weigh 65kg
My BMI is 21.2
GoodBye
*/
νμ₯ ν¨μλ κΈ°μ‘΄ ν΄λμ€μ λ©€λ² ν¨μμΈ κ²μ²λΌ νΈμΆν μ μμ λΏ, μ€μ λ‘ ν΄λΉ ν΄λμ€μ λ©€λ²μΈ κ²μ μλκΈ° λλ¬Έμ privateμ΄λ protected λ©€λ²μλ μ κ·Όν μ μλ€. μλ₯Ό λ€μ΄, μ μ½λμμ Person κ°μ²΄μ memerNumber νλ‘νΌν°μλ νμ₯ ν¨μκ° μ κ·Όν μ μλ€.
#1-3
fun main() {
val hongGilDong = Person("νκΈΈλ", 175, 65, 3579)
hongGilDong.introduceType()
}
interface Animal {
val type : String
}
class Person (val name: String, val height: Int, val weight: Int, private val memberNumber : Int) : Animal {
override val type = "mammal"
// ...
}
fun Animal.introduceType() {
println("My type is ${this.type}")
}
/* μΆλ ₯ κ²°κ³Ό
My type is mammal
*/
μ΄κ³Ό κ°μ΄ μΈν°νμ΄μ€μλ νμ₯ ν¨μλ₯Ό μ μ©ν μ μλ€.
#2 μμ½
νμ₯ ν¨μλ₯Ό ν΅ν΄, μλμ μ½λλ₯Ό 건λλ¦¬μ§ μκ³ κΈ°λ₯μ μΆκ°νλ€.
#3 μ΄ κ°λ μ΄ μ¬μ©λ κΈ
[λ°±μ€] 10845 - ν
#1 μκ³ λ¦¬μ¦ java.util.Stackκ³Όλ λ¬λ¦¬ java.util.Queueλ ν΄λμ€κ° μλλΌ μΈν°νμ΄μ€λ€. λλ java.util.Queue μΈν°νμ΄μ€λ₯Ό ꡬνν ν΄λμ€μΈ LinkedListλ₯Ό Queueλ‘μ μ¬μ©νλ€. last()λ νμ₯ ν¨μ(Extension functions)
kenel.tistory.com
[λ°±μ€] 2606 - λ°μ΄λ¬μ€
#1 μκ³ λ¦¬μ¦ #2 μ½λ (μ½νλ¦°) fun main() { readln() // μ»΄ν¨ν°μ κ°―μλ νμ΄μ μ¬μ©νμ§ μλλ€. val connectionCount = readln().toInt() val connections : ArrayList = ArrayList() for(i : Int in 0..
kenel.tistory.com
'κΉ¨μ κ°λ π > Kotlin' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Kotlin] λλ€(Lambda) ννμ (0) | 2024.02.01 |
---|---|
[Kotlin] ν¨μ νμ (Fuction types) ννμ (0) | 2024.01.31 |
[Kotlin] ν¨μν μΈν°νμ΄μ€ (Single Abstract Method Interface) (0) | 2024.01.30 |
[Kotlin] μ°μ°μ μ€λ²λ‘λ© (Operator overloading) (0) | 2024.01.29 |
[Kotlin] νλ‘νΌν°(Property) (0) | 2024.01.17 |