Kotlinμ 곡λΆνλ©΄μ μμμ λν λΆλΆμ 곡λΆνκ³ μ 곡μ λ¬Έμλ₯Ό μ§μ λ²μνλ€.
ν΄λΉ λ¬Έμλ 2022λ
3μ 3μΌμ 곡μ λ¬Έμμ΄λ€.
μ€κ°μ€κ° π νμκ° μλ λ¬Έμ₯μ΄λ μμ λͺ©μ λ²μμ΄ λΆμ νν μ μμΌλ©° ꡬκΈμ΄λ ννκ³ λ²μκΈ°μ λμμ λ°μλ€.
Kotlin 곡μλ¬Έμ νμΈνκΈ° β‘οΈ
β» μμ΄ μ 곡μλ ν΄μΈ μ ννλ μλκΈ°μ λ²μμλ μμ, μ€μ, κ΅¬κΈ λ²μμ΄ λ¬΄μν λ§μ μ μμΌλ©°, νΌμ 곡μλ¬Έμλ₯Ό μ°Έμ‘°ν΄κ°λ©° λ²μνλ€ λ³΄λ μ€νλ λ§μ μ μλ€. μ νν λ΄μ©μ 곡μλ¬Έμλ₯Ό μ§μ μ΄ν΄λ³΄κ±°λ λ€λ₯Έ μ 보λ€μ λ μ°Ύμ보λ κ²μ μΆμ²νλ€.
(νμ§λ§ λκΈ νΌλλ°±λ νμν©λλ€π )
Interfaces in Kotlin can contain declarations of abstract methods, as well as method implementations. What makes them different from abstract classes is that interfaces cannot store a state. They can have properties, but these need to be abstract or provide accessor implementations.
μ½νλ¦°μ μΈν°νμ΄μ€λ μΆμ λ©μλ μ μΈκ³Όμ λ©μλ ꡬν λͺ¨λ ν¬ν¨ν μ μλ€. μΆμ ν΄λμ€μμ μ°¨μ΄μ μ μΈν°νμ΄μ€λ μνλ₯Ό μ μ₯ν μ μλ€λ κ²μ΄λ€. μΈν°νμ΄μ€λ μμ±μ κ°μ§ μ μμ§λ§ μΆμμ΄κ±°λ μ κ·Όμ ꡬνμ΄ μ 곡λμ΄μΌνλ€.
An interface is defined using the keyword interface:
μΈν°νμ΄μ€λ interface ν€μλλ‘ μ μλλ€:
Implementing interfaces
A class or object can implement one or more interfaces:
ν΄λμ€λ κ°μ²΄λ νλ νΉμ κ·Έ μ΄μμ μΈν°νμ΄μ€λ₯Ό ꡬνν μ μλ€:
Properties in interfaces
You can declare properties in interfaces. A property declared in an interface can either be abstract or provide implementations for accessors. Properties declared in interfaces can't have backing fields, and therefore accessors declared in interfaces can't reference them:
μΈν°νμ΄μ€μ μμ±μ μ μΈν μ μλ€. μΈν°νμ΄μ€μ μ μΈλ μμ±μ μΆμμ΄κ±°λ μ κ·Όμλ₯Ό μν ꡬνμ΄ μ 곡λμ΄μΌνλ€. μΈν°νμ΄μ€μ μ μΈλ μμ±λ€μ backing νλλ₯Ό κ°μ§ μ μκ³ κ·Έλ κΈ° λλ¬Έμ μΈν°νμ΄μ€μ μ μΈλ μ κ·Όμλ μ κ·Όν μ μλ€.
interface MyInterface {
val prop: Int // abstract
val propertyWithImplementation: String
get() = "foo"
fun foo() {
print(prop)
}
}
class Child : MyInterface {
override val prop: Int = 29
}
Interfaces Inheritance
An interface can derive from other interfaces, meaning it can both provide implementations for their members and declare new functions and properties. Quite naturally, classes implementing such an interface are only required to define the missing implementations:
μΈν°νμ΄μ€λ λ€λ₯Έ μΈν°νμ΄μ€λ‘λΆν° νμλ μ μλ€. μ¦, λ©€λ²λ₯Ό μν ꡬνμ μ 곡νλ κ²κ³Ό μλ‘μ΄ ν¨μμ μμ±μ μ μΈνλ κ² λ λ€ κ°λ₯ν κ²μ μλ―Ένλ€. λΉμ°ν κ·Έλ¬ν μΈν°νμ΄μ€λ₯Ό ꡬνν ν΄λμ€λ ꡬνλμ§ μμ λΆλΆλ§ μ μνλ κ²λ§ μꡬλλ€.
interface Named {
val name: String
}
interface Person : Named {
val firstName: String
val lastName: String
override val name: String get() = "$firstName $lastName"
}
data class Employee(
// implementing 'name' is not required
override val firstName: String,
override val lastName: String,
val position: Position
) : Person
Resolving overriding conflicts
When you declare many types in your supertype list, you may inherit more than one implementation of the same method:
μνΌνμ λͺ©λ‘μμ λ§μ νμ λ€μ μ μΈν λ λμΌ λ©μλμ λν΄ νλ μ΄μμ ꡬνμ μμν μ§ λͺ¨λ₯Έλ€:
interface A {
fun foo() { print("A") }
fun bar()
}
interface B {
fun foo() { print("B") }
fun bar() { print("bar") }
}
class C : A {
override fun bar() { print("bar") }
}
class D : A, B {
override fun foo() {
super<A>.foo()
super<B>.foo()
}
override fun bar() {
super<B>.bar()
}
}
Interfaces A and B both declare functions foo() and bar(). Both of them implement foo(), but only B implements bar() (bar() is not marked as abstract in A, because this is the default for interfaces if the function has no body). Now, if you derive a concrete class C from A, you have to override bar() and provide an implementation.
μΈν°νμ΄μ€ Aμ B λͺ¨λ ν¨μ foo()μ bar()λ₯Ό μ μΈνλ€. λͺ¨λ foo()λ ꡬννμ§λ§ Bλ§ bar()λ₯Ό ꡬννλ€(bar()λ Aμμ abstractμΌλ‘ νμλμ§ μμλλ° μ΄κ²μ΄ μΈν°νμ΄μ€μμ ν¨μμ λ°λκ° μμ λμ κΈ°λ³Έκ°μ΄κΈ° λλ¬Έμ΄λ€). μ΄μ Aλ‘λΆν° ν΄λμ€ Cλ₯Ό νμνλ €λ©΄ bar()λ₯Ό μ€λ²λΌμ΄λνκ³ κ΅¬νμ μ 곡ν΄μΌνλ€.
However, if you derive D from A and B, you need to implement all the methods that you have inherited from multiple interfaces, and you need to specify how exactly D should implement them. This rule applies both to methods for which you've inherited a single implementation (bar()) and to those for which you've inherited multiple implementations (foo()).
κ·Έλ°λ° Dλ₯Ό Aμ Bλ‘λΆν° νμνλ €λ©΄ λ€μμ μΈν°νμ΄μ€μμ μμλ°μ λͺ¨λ λ©μλλ€μ ꡬνν΄μΌνκ³ Dκ° μ΄λ»κ² ꡬ체μ μΌλ‘ κ·Έλ€μ ꡬννλμ§ λͺ μν΄μΌνλ€. μ΄ κ·μΉμ bar()κ³Ό κ°μ΄ λ¨μΌ ꡬνμ μμν λ©μλμ foo()κ°μ΄ μ¬λ¬ ꡬνμ μμν λ©μλ λͺ¨λμ μ μ©λλ€.
'Android & Kotlin' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[λ΄λ³΄λ΄λ²] Properties | Kotin (0) | 2022.03.03 |
---|---|
[λ΄λ³΄λ΄λ²] Classes | Kotlin (0) | 2022.03.03 |
[λ΄λ³΄λ΄λ²] Inheritance | Kotlin (0) | 2022.03.02 |
Overloading / Overriding (0) | 2022.03.02 |
Lesson 6: App Architecture (Persistence) (0) | 2022.02.23 |