2021. 4. 23. 19:17ㆍKotlin/문법
open class Base{
init{
Log.i(“KMJ”, “this is Base Class”);
}
constructor(b1:String,b2:String){
Log.i(“KMJ”, “this is Base Class Constructor with 2param $b1/$b2”);
}
constructor(b1:String,b2:String,b3:String){
Log.i(“KMJ”, “this is Base Class Constructor with 3param $b1/$b2/$b3”);
}
}
class Topping:Base{
init{
Log.i(“KMJ”, “this is Topping Class”);
}
constructor(b1:String,b2:String):super(b1,b2){
Log.i(“KMJ”, “this is Topping Class Constructor with 2param $b1/$b2”);
}
constructor(b1:String,b2:String,b3:String):super(b1,b2,b3){
Log.i(“KMJ”, “this is Topping Class Constructor with 3param $b1/$b2/$b3”);
}
}
val topping2=Topping(“topping1”, “topping2”);
val topping3=Topping(“topping1”, “topping2”, “topping3”);
- 초기화 순서 : Base Class(primary constructor(property, init) → sub constructor) → Topping Class(primary constructor(property, init) → sub constructor)
2020-10-29 15:38:43.101 13520-13520/com.example.kotlinstudy I/KMJ: this is Base Class
2020-10-29 15:38:43.101 13520-13520/com.example.kotlinstudy I/KMJ: this is Base Class Constructor with 2 param topping1 / topping2
2020-10-29 15:38:43.101 13520-13520/com.example.kotlinstudy I/KMJ: this is Topping Class
2020-10-29 15:38:43.101 13520-13520/com.example.kotlinstudy I/KMJ: this is Topping Class Constructor with 2 param topping1 / topping2
2020-10-29 15:38:43.101 13520-13520/com.example.kotlinstudy I/KMJ: this is Base Class
2020-10-29 15:38:43.101 13520-13520/com.example.kotlinstudy I/KMJ: this is Base Class Constructor with 3 param topping1 / topping2 / topping3
2020-10-29 15:38:43.101 13520-13520/com.example.kotlinstudy I/KMJ: this is Topping Class
2020-10-29 15:38:43.101 13520-13520/com.example.kotlinstudy I/KMJ: this is Topping Class Constructor with 3 param topping1 / topping2 / topping3
- primary constructor : Topping Class에 primary constructor가 있는 경우 Base Class도 primary constructor를 명시해주어야 함
open class Base(p: Int)
class Derived(p: Int) : Base(p)
- 기본 생성자 : sub constructor 생성 시 자동 생성되지 않으므로 선언 필요(Base Class가 sub constructor을 가지고 있는 경우에는 Topping Class의 sub constructor에서 명시적 위임을 해줘야 함)
- 기본 생성자만 있는 경우, Base Class에 ()를 선언하든 안하든 상관없음(but empty primary constructor not recommended)
open class Base{ // Base()도 상관없음
…
}
class Topping : Base(){
…
}
val topping = Topping();
2. 암시적인 기본 생성자 위임(Base Class에 sub constructor 있는 경우 에러)
open class Base{
init{
Log.i(“KMJ”, “this is Base Class”);
}
}
class Topping : Base{
init{
Log.i(“KMJ”, “this is Topping Class”);
}
constructor(b1 : String, b2 : String){ // Topping의 기본 생성자 위임이 생략됨(this())
Log.i(“KMJ”, “this is Topping Class Constructor with 2 param $b1 / $b2”);
}
constructor(b1 : String, b2 : String, b3 : String){ // Topping의 기본 생성자 위임이 생략됨(this())
Log.i(“KMJ”, “this is Topping Class Constructor with 3 param $b1 / $b2 / $b3”);
}
}
val topping = Topping(); // 에러
val topping1 = Topping(“topping1”, “topping2”);
val topping2 = Topping(“topping1”, “topping2”, “topping3”);
2020-10-29 17:48:42.567 16403-16403/com.example.kotlinstudy I/KMJ: this is Base Class
2020-10-29 17:48:42.567 16403-16403/com.example.kotlinstudy I/KMJ: this is Topping Class
2020-10-29 17:48:42.567 16403-16403/com.example.kotlinstudy I/KMJ: this is Topping Class Constructor with 2 param topping1 / topping2
2020-10-29 17:48:42.567 16403-16403/com.example.kotlinstudy I/KMJ: this is Base Class
2020-10-29 17:48:42.567 16403-16403/com.example.kotlinstudy I/KMJ: this is Topping Class
2020-10-29 17:48:42.567 16403-16403/com.example.kotlinstudy I/KMJ: this is Topping Class Constructor with 3 param topping1 / topping2 / topping3
'Kotlin > 문법' 카테고리의 다른 글
by (0) | 2022.02.24 |
---|---|
기본 문법 간단 리뷰 (1) | 2021.08.11 |
Class 생성과 위임 (0) | 2021.04.23 |