richodemus
10/24/2016, 6:34 AMclass MyClass2(private val constructor_parameter: String) {
private val initialized_by_class: String
init {
initialized_by_class = "asdasdasd"
}
}
richodemus
10/24/2016, 6:35 AMteedee
10/24/2016, 6:50 AMrichodemus
10/24/2016, 6:53 AMteedee
10/24/2016, 6:57 AMteedee
10/24/2016, 6:58 AMrichodemus
10/24/2016, 6:59 AMelect
10/24/2016, 7:03 AMvar
and val
), use only secondary constructors and write there the code you want to write in the constructorelect
10/24/2016, 7:04 AMelect
10/24/2016, 7:04 AMclass MyClass {
val myObject: SomeObjectThatNeedsCodeToInitialize
constructor(whatever: Any) {
//do what you need and init myObject
}
constructor(whatever2: Any2) {
//do what you need and init myObject
}
}
teedee
10/24/2016, 7:05 AMteedee
10/24/2016, 7:07 AMrichodemus
10/24/2016, 7:08 AMclass MyClass2() {
private val constructor_parameter: String
private val initialized_by_class: String
constructor(param:String) : this() {
this.constructor_parameter = param
this.initialized_by_class = "asdasd"
}
}
here it complains that both fields can't be reassignedteedee
10/24/2016, 7:09 AMteedee
10/24/2016, 7:09 AMelect
10/24/2016, 7:10 AMclass MyClass2 {
private val constructor_parameter: String
private val initialized_by_class: String
constructor(asd:String) {
this.constructor_parameter = asd
this.initialized_by_class = "asdasd"
}
}
elect
10/24/2016, 7:10 AMMyClass2()
, ()
stands for primary constructorrichodemus
10/24/2016, 7:11 AMelect
10/24/2016, 7:11 AMrichodemus
10/24/2016, 7:11 AMrichodemus
10/24/2016, 7:11 AMelect
10/24/2016, 7:12 AMrichodemus
10/24/2016, 7:13 AMteedee
10/24/2016, 7:13 AMrichodemus
10/24/2016, 7:14 AMelect
10/24/2016, 7:14 AMinit{}
, if you have secondary constructor, you do it naturally thereelect
10/24/2016, 7:14 AMrichodemus
10/24/2016, 7:15 AMelect
10/24/2016, 7:15 AMteedee
10/24/2016, 7:16 AM