λ°κ²½μ†‘μ˜ E.B

Android <3> Classes and objects λ³Έλ¬Έ

GDSC/Android

Android <3> Classes and objects

m_gyxxmi 2023. 11. 3. 22:20

🧣 πŸ«₯ 11μ›”μ΄λ‹ˆκΉŒ!!

 

 

---μƒμ„±μž λ‚΄μš© μ •λ¦¬λž€-----

 


 

🧣 Inheritance(상속)

: 기쑴의 클래슀λ₯Ό " ν™•μž₯ " ν•œ μƒˆλ‘œμš΄ 클래슀 [μž¬μ‚¬μš© λ©”μ»€λ‹ˆμ¦˜]

 

 

 

πŸ«₯ 상속+a λ°”λ‘œ μ•ŒκΈ°

 

* μƒμ†μ˜ λŒ€μƒλ“€ 쀑, 객체λ₯Ό 생성할 수 μžˆλŠ” 것은 λ¬΄μ—‡μΌκΉŒ?

: 객체 생성 = λͺ¨λ‘(κ°’, λ™μž‘) κ΅¬ν˜„λœ 것 = 일반 클래슀만 κ°€λŠ₯

 


 

πŸ«₯ 단일 상속과 닀쀑 상속

* 코틀린은 λ‹¨μΌμƒμ†, λ‹€μ€‘κ΅¬ν˜„μ΄ κ°€λŠ₯ν•œ μ–Έμ–΄λ‹€!

 

단일 상속 : μ–΄λ–€ ν΄λž˜μŠ€κ°€ λ‹€λ₯Έ 클래슀λ₯Ό ν•œ λ²ˆμ— ν•˜λ‚˜λ§Œ μƒμ†ν•˜λŠ” 경우

닀쀑 상속 : μ–΄λ–€ ν΄λž˜μŠ€κ°€ 두 개 μ΄μƒμ˜ ν΄λž˜μŠ€λ‘œλΆ€ν„° μƒμ†λ°›λŠ” 경우

 

-> 닀쀑 상속이 μ•ΌκΈ°ν•  수 μžˆλŠ” 문제 "닀이아λͺ¬λ“œ 상속 문제"

 


 

 

🧣 κΈ°μ‘΄ 클래슀 ν™•μž₯의 방법

1) Override : λ³€κ²½

2) MEM ν”„λ‘œνΌν‹° μΆ”κ°€

 

 

πŸ«₯ open ν‚€μ›Œλ“œ

  • μ½”ν‹€λ¦°μ˜ ν΄λž˜μŠ€λŠ” κΈ°λ³Έμ μœΌλ‘œ μƒμ†μ΄ λΆˆκ°€λŠ₯   =>  open ν‚€μ›Œλ“œλ‘œ 상속이 κ°€λŠ₯ν•˜λ„λ‘ ν•΄μ€˜μ•Ό 함.
  • 좔상(abstract) 클래슀 항상 open => open ν‚€μ›Œλ“œλ₯Ό λͺ…μ‹œν•˜μ§€ μ•Šμ•„λ„ 됨.

 

-예제

 

μƒμœ„ 클래슀인 Aquarium μƒμ„±ν•˜κΈ°

open class Aquarium(open var length: Int = 100, open var width: Int = 20, open var height: Int = 40) {
    open var volume: Int
        get() = width * height * length / 1000
        set(value) {
            height = (value * 1000) / (width * length)
        }
    open val shape = "rectangle" 
    open var water: Double = 0.0
        get() = volume * 0.9

    fun printSize() {
        println(shape)
        println("Width: $width cm " +
                "Length: $length cm " +
                "Height: $height cm ")
        // 1 l = 1000 cm^3
        println("Volume: $volume liters Water: $water liters (${water / volume * 100.0}% full)")
    }
}

fun buildAquarium() {
    val aquarium6 = Aquarium(length = 25, width = 25, height = 40)
    aquarium6.printSize()
}

fun main() {
    buildAquarium()
}

: shape, volume 등을 ν”„λ‘œνΌν‹°λ‘œ κ°–λŠ” Aquarium 클래슀λ₯Ό μƒμ„±ν•΄λ΄€μˆ©λ‹€

 

 

Aquarium을 상속 λ°›λŠ” TowerTank 클래슀 μƒμ„±ν•˜κΈ°

class TowerTank (override var height: Int, var diameter: Int): Aquarium(height = height, width = diameter, length = diameter) {
    override var volume: Int
    // ellipse area = π * r1 * r2
    get() = (width/2 * length/2 * height / 1000 * PI).toInt()
    set(value) {
        height = ((value * 1000 / PI) / (width/2 * length/2)).toInt()
    }

    override var water = volume * 0.8
    override val shape = "cylinder"
}

 

: height ν”„λ‘œνΌν‹°μ˜ 경우, constructor(μƒμ„±μž) μ•ˆμ—μ„œ μ •μ˜λ˜μ—ˆκΈ° 문에, 이λ₯Ό μƒμ†λ°›μœΌλ €λ©΄ override ν‚€μ›Œλ“œλ₯Ό μ•žμ— λΆ™μ—¬μ€˜μ•Ό 함! -> λͺ…μ‹œμ μœΌλ‘œ 선언돼야 함.

*반면, diameter ν”„λ‘œνΌν‹°μ˜ 경우, μƒμœ„ 클래슀(Aquarium)μ—μ„œ μ •μ˜λœ 것이 μ•„λ‹ˆλ‹ˆκΉŒ override μ•ˆ 뢙여도 됨~

 

 


 

 

πŸ«₯ μΆ”μƒν΄λž˜μŠ€μ˜ ν”„λ‘œνΌν‹°/ν•¨μˆ˜ 

  1. abstract ν‚€μ›Œλ“œλ₯Ό ν‘œμ‹œ X : 좔상X -> ν•˜μœ„ ν΄λž˜μŠ€κ°€  ν”„λ‘œνΌν‹°/λ©”μ„œλ“œλ₯Ό κ·ΈλŒ€λ‘œ μ‚¬μš©O ; override둜 변경도 κ°€λŠ₯
  2. abstract ν‚€μ›Œλ“œλ₯Ό ν‘œμ‹œ O : ν•˜μœ„ ν΄λž˜μŠ€μ—μ„œ λ°˜λ“œμ‹œ κ΅¬ν˜„λ˜μ–΄μ•Ό 함. (좔상 멀버 ꡬ체화) ; override둜 κ΅¬ν˜„ ν•„μˆ˜

 

 

πŸ«₯ μΈν„°νŽ˜μ΄μŠ€

  • ν΄λž˜μŠ€λŠ” μΈν„°νŽ˜μ΄μŠ€μ—μ„œ μ •μ˜λœ λ©”μ„œλ“œλ₯Ό κ΅¬ν˜„ν•¨μœΌλ‘œμ¨ κΈ°λŠ₯ ν™•μž₯ κ°€λŠ₯
  • 닀쀑 μΈν„°νŽ˜μ΄μŠ€ κ΅¬ν˜„ κ°€λŠ₯ ( *ν΄λž˜μŠ€λŠ” 단일 μƒμ†λ§Œ κ°€λŠ₯ but μ—¬λŸ¬ μΈν„°νŽ˜μ΄μŠ€ κ΅¬ν˜„ κ°€λŠ₯)

 

 

-예제

interface FishAction  {
    fun eat()
}

class Shark: AquariumFish(), FishAction {
    override val color = "grey"
    override fun eat() {
        println("hunt and eat fish")
    }
}
class Plecostomus: AquariumFish(), FishAction {
    override val color = "gold"
    override fun eat() {
        println("eat algae")
    }
}

 

: Shark와 Plecostomus 클래슀 λͺ¨λ‘ FishAction μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•˜κ³  μžˆλ‹€μ•…,,

 

 


 

πŸ§£κ·Έλ ‡λ‹€λ©΄ 일반 클래슀, 좔상 클래슀, μΈν„°νŽ˜μ΄μŠ€λŠ” μ–Έμ œ μ‚¬μš©ν•˜λŠ”κ°€?

license : Apache 2 license, Google developers Training

 

 


 

----- extension func κ³Ό mem funx λΉ„κ΅λž€ ----

 

 

 

 

μ‹œν—˜μ΄ κΏ‘λ‚¬μŠ΅.. ν•˜ μ „ μ‹œν—˜κΈ°κ°„μ΄ λ„˜ μ’‹μŠ΄λ‹€... 정말 λ”± μ‹œν—˜ κ³΅λΆ€λ§Œ ν•˜λ©΄ λ˜λ‹ˆκΉŒ............................γ…œ.............. 

ν˜Ήμ‹œ μšΈκ³ μžˆλƒκ΅¬μš”.. λ„€λ‹ˆμ˜€... ^.... ^ 

자꾸 λ‹¨μ›λ§ˆλ‹€ 파트 λͺ‡ κ°œμ”© 빼먹게 λ˜λŠ”λ°,,, 11μ›” μ•ˆμ— λ‹€ μ±„μš°λ„λ‘ ..ν•˜κ² μŠ΅λ‹ˆλ‹€ νŒŒμ΄νŒ….>!