34 lines
798 B
Plaintext
34 lines
798 B
Plaintext
Classes can be nested in other classes:
|
|
{jet}
|
|
class Outer() {
|
|
private val bar : Int = 1
|
|
class Nested() {
|
|
fun foo() = 2
|
|
}
|
|
}
|
|
|
|
val demo = Outer.Inner().foo() // == 2
|
|
{jet}
|
|
|
|
h3. Inner classes
|
|
|
|
A class may be marked as *inner* to be able to access members of outer class. Inner classes carry a reference to an object of an outer class:
|
|
|
|
{jet}
|
|
class Outer() {
|
|
private val bar : Int = 1
|
|
inner class Inner() {
|
|
fun foo() = bar
|
|
}
|
|
}
|
|
|
|
val demo = Outer().Inner().foo() // == 1
|
|
{jet}
|
|
|
|
See [Qualified *this* expressions|This expressions#Qualified] to learn about disambiguation of *this* in inner classes.
|
|
|
|
{note:title=Inner classes are under development}See the corresponding [issue|http://youtrack.jetbrains.com/issue/KT-1174].{note}
|
|
|
|
h3. What's next
|
|
|
|
* [Object expressions and Declarations] |