CLOVIS
09/19/2022, 12:02 PMabstract class A {
abstract inner class B : A() {
init {
// How can I access the outer class?
}
}
}
In that example, this
refers to the class B
, this@A
refers to the parent class. How can I access the outer class?Marcus Brito
09/19/2022, 12:04 PMval
in the proper scope:
abstract class A {
val a = this;
abstract inner class B : A() {
init {
// use `a` here
}
}
}
CLOVIS
09/19/2022, 12:07 PMCLOVIS
09/19/2022, 12:10 PMCLOVIS
09/19/2022, 12:21 PMVampire
09/19/2022, 12:29 PMprivate val
, then it works. Because you cannot access it from subclass, but within that scope.CLOVIS
09/19/2022, 12:32 PMRuckus
09/19/2022, 2:40 PMIn that example,Are you sure? It seems to work fine for me.refers to the classthis
,B
refers to the parent class. How can I access the outer class?this@A
this@A
is referring to the outer class, and super
is referring to the parent.Vampire
09/19/2022, 2:46 PMRuckus
09/19/2022, 2:48 PMval a = this
because the child will also get that field. But if I forgo that and use this@A
as I said it works fine for me.
https://pl.kotl.in/-5TZyaH2nVampire
09/19/2022, 3:54 PM