Nested Classes(嵌套类与内部类)

1
2
3
4
5
6
7
8
class Outer {
private val bar: Int =1
class Nested {
fun foo() = 2
}
}
val demo = Outer.Nested().foo() // =2
1
2
3
4
5
6
7
8
class Outer {
private val bar: Int = 1
inner class Inner {
fun foo() = bar
}
}
val demo = Outer().Inner().foo()// ==1

匿名内部类

1
2
3
window.addMouseListener(object: MouseAdapter()) {
override fun mouseClicked(e: MouseEvent) {...}
}