๐Ÿ“ฆ Chango/๐Ÿฃ EDOC

[AndroidStudio / Kotlin] ์ฝ”ํ‹€๋ฆฐ ๊ณ ๊ธ‰๋ฌธ๋ฒ•

์„ ๋‹ฌ 2021. 4. 30. 18:59
๋ฐ˜์‘ํ˜•

1. Lamda

: ๋žŒ๋‹ค์‹์€ ์ต๋ช…ํ•จ์ˆ˜ -> value์ฒ˜๋Ÿผ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ์Œ.

 

1) ๋ฉ”์†Œ๋“œ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„˜๊ฒจ์ค„ ์ˆ˜ ์žˆ์Œ. fun maxBy(a: Int)

2) return ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ.

 

1-1. ๋žŒ๋‹ค์˜ ๊ธฐ๋ณธ์ •์˜

val lamdaName : Type = {argumentList -> codeBody}

val square : (Int) -> (Int) = {number -> number*number}

val nameAge = {name : String, age : Int ->
    "my name is ${name}, I'm ${age}"
}

fun main(){
    println(square(12))
    println(nameAge("whkakrkr", 21))
}

 

1-2. ๋žŒ๋‹ค์™€ ํ™•์žฅํ•จ์ˆ˜

val pizzaIsGreat : String.() -> String = {
    this + "Pizza is the best!"
}

fun extendString(name:String, age:Int) : String {
    val introduceMyself : String.(Int) -> String = {"I am ${this} and ${it} years old."}
    return name.introduceMyself(age)
}

 

1-3. ๋žŒ๋‹ค์˜ ๋ฆฌํ„ด

val calculateGrade : (Int) -> String = {
    when(it){
        in 0..40 -> "fail"
        in 41..70 -> "pass"
        in 71..100 -> "perfect"
        else -> "huh?"
    }
}

 

1-4. ๋žŒ๋‹ค๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐฉ๋ฒ•

fun invokeLamda(lamda : (Double) -> Boolean) : Boolean {
    return lamda(5.2343)
}

fun main(){
	val lamda = {number : Double ->
        number == 4.3213
    }

    println(invokeLamda(lamda))
    println(invokeLamda({it > 3.22}))
    println(invokeLamda { it > 3.22 })
}

 

๋ฒ„ํŠผ์— ๋žŒ๋‹ค์‹ ์‚ฌ์šฉ

1 ) kotlin ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์•„๋‹Œ ์ž๋ฐ” ์ธํ„ฐํŽ˜์ด์Šค

2 ) ๊ทธ ์ธํ„ฐํŽ˜์ด์Šค๋Š” ๋”ฑ ํ•˜๋‚˜์˜ ๋ฉ”์†Œ๋“œ๋งŒ ๊ฐ€์ ธ์•ผํ•จ

 

2. Data Class

data class Ticket(val companyName :String, val name :String, var date :String, var seatNumber :Int)
class TicketNormal(val companyName :String, val name :String, var date :String, var seatNumber :Int)

// toString(), hashCode(), equals(), copy()

fun main() {
    val ticketA = Ticket( "KoreanAir", "whk", "2001-06-13",13)
    val ticketB = TicketNormal( "KoreanAir", "whk", "2001-06-13",13)

    println(ticketA)    //Ticket(companyName=KoreanAir, name=whk, date=2001-06-13, seatNumber=13)
    println(ticketB)    //com.example.practice_codewithjoyce.TicketNormal@61bbe9ba
}

 

3. Companion object

class Book private constructor(val id : Int, val name : String) {
    
    companion object BookFactory  : IdProvider {
        
        override fun getId() : Int {
            retrurn 444
        }
        
        val myBook = "new book"
        
        fun create() = Book( 0, myBook
    }
}

interface IdProvider {
    fun getId() : Int
}

fun main(){
    val book = Book.Companion.create()
    
    val bookId = Book.BookFactory.getId()
    println("${book.id} ${book.name}")
}

 

4. Object

//Singleton Pattern
object CarFactory {
    val cars = mutableListOf<Car>()
    
    fun makeCar(horsePower : Int) : Car {
        val car = Car(horsePower)
        cars.add(car)
        return car
    }
}

data class Car(val horsePower : Int)

fun main() {
    val car : Car = CarFactory.makeCar(10)
    val car2 : Car = CarFactory.makeCar(200)
    
    println(car)
    println(car2)
    println(CarFactory.cars.size.toString())
}
๋ฐ˜์‘ํ˜•