Ray Ryan
03/23/2020, 8:42 PMclass Outer {
abstract class Inner {
}
}
class Child extends Outer.Inner {
Child(Outer outer) {
// Calls Inner constructor, providing
// outer as the containing instance
outer.super();
}
}
I can’t find an equivalent in Kotlin, if I want Child
to live in a separate file / module.
The auto converter generates this, which doesn’t compile:
class Outer {
abstract inner class Inner
}
class Child(outer: Outer?) : Inner()
Casey Brooks
03/23/2020, 8:59 PMInner
, Outer
, and Child
more explicit understandable. You can use the with
scoping function within Inner
to make it access members of Outer
in a similar way to an inner classclass Outer {
var outerStringProperty = ""
open class Inner(outer: Outer, someConstructorProperty: String) {
init {
with(outer) {
outerStringProperty = someConstructorProperty
}
}
}
}
class Child(outer: Outer) : Outer.Inner(outer, "property")
fun createChild(): Child {
val outer = Outer()
return Child(outer)
}
Ray Ryan
03/23/2020, 9:03 PMBenjamin Charais
03/24/2020, 9:00 PMinner
qualifier on your kotlin inner class, this means it will not have direct access to parent methods/properties, but will compile
class Outer {
abstract class Inner
}
class Child(outer: Outer) : Outer.Inner() {
init {
// Do work with outer if need be
}
}
Ray Ryan
03/24/2020, 9:06 PMBenjamin Charais
03/24/2020, 9:24 PM