1. Abstract Class
추상 Class를 구현(상속)하는 Class
abstract class Design{
abstract fun drawText()
abstract fun draw()
fun showWindow(){
}
}
class Implements : Design{
override fun drawText() {
TODO("Not yet implemented")
}
override fun draw() {
TODO("Not yet implemented")
}
}
2. Interface
1) interface 구문. 모든 요소가 abstract 생략됨.
2) interface 상속 시 () 사용 안 함.
3) 속성도 overriding 한다.
interface IAnimal{
fun say()
fun walk()
fun eat()
}
class Dog: IAnimal{
override fun say() {
TODO("Not yet implemented")
}
override fun walk() {
TODO("Not yet implemented")
}
override fun eat() {
TODO("Not yet implemented")
}
}
interface InterfaceKotlin{
var variable: String
fun get()
fun set()
}
class KotlinImpl : InterfaceKotlin{
override var variable: String = "초기값"
override fun get(){
}
override fun set(){
}
}
4) Android Programming에서 자주 사용하는 코드
var kotlinIple = object : InterfaceKotlin{
override var variable: String = "init"
override fun get(){}
override fun set(){}
}
3. 추상 Class 설계
1) 구현. 기본 생성자를 호출할 수 있는 작업까지 해야 함.
interface InterfaceKotlin1 {
var variable: String
fun get()
fun set()
}
2) 설계
class KotlinImpl1 : InterfaceKotlin1{
override var variable: String = ""
override fun get() {
TODO("Not yet implemented")
}
override fun set() {
TODO("Not yet implemented")
}
}
3) 접근 제한자를 test를 위한 부모 class
public 접근자 사용 안 하고 4가지.
open class Parent2{
private val privateVal = 1
protected open val protectedVal = 2
internal val internalVal = 3
val defaultVal = 4
}
class Child2 : Parent2(){
fun callVariables(){
// privateVal private 접근 안 됨.
println("${protectedVal}")
println("${internalVal}")
println("${defaultVal}")
}
}
'Language > Kotlin' 카테고리의 다른 글
[Kotlin] Lateinit, Lazy (0) | 2022.02.18 |
---|---|
[Kotlin] Generic, Null (0) | 2022.02.18 |
[Kotlin] 상속과 확장 (0) | 2022.02.17 |
[Kotlin] 함수 (0) | 2022.02.17 |
[Kotlin] collection(list, map, set) (0) | 2022.02.17 |