74 lines
2.2 KiB
Plaintext
74 lines
2.2 KiB
Plaintext
Sometimes we need to create an object of a slight modification of some class, without explicitly declaring a new subclass for it. *Java* handles this case with _anonymous inner classes_. [Kotlin] slightly generalizes this concept with *object* expressions and *object* declarations.
|
|
|
|
h3. Object expressions
|
|
|
|
To create an object of an anonymous class that inherits from some type (or types), one writes:
|
|
{jet}
|
|
window.addMouseListener(object : MouseAdapter() {
|
|
override fun mouseClicked(e : MouseEvent) {
|
|
// ...
|
|
}
|
|
|
|
override fun mouseEntered(e : MouseEvent) {
|
|
// ...
|
|
}
|
|
})
|
|
{jet}
|
|
If a supertype has a constructor, appropriate constructor parameters must be passed to it. Many supertypes may be specified as a comma-separated list after the colon:
|
|
{jet}
|
|
open class A(x : Int) {
|
|
public virtual val y : Int = x
|
|
}
|
|
|
|
open class B(s : String)
|
|
|
|
val ab = object : A(1), B("abc") {
|
|
override val y = 15
|
|
}
|
|
{jet}
|
|
|
|
If, by any chance, we need "just an object", with no nontrivial supertypes, we can simply say:
|
|
{jet}
|
|
val adHoc = object {
|
|
var x : Int = 0
|
|
var y : Int = 0
|
|
}
|
|
|
|
print(adHoc.x + adHoc.y)
|
|
{jet}
|
|
|
|
h3. Object declarations
|
|
|
|
[Singleton|http://en.wikipedia.org/wiki/Singleton_pattern] is a very useful pattern, and [Kotlin] (after *Scala*) makes it easy to declare singletons:
|
|
{jet}
|
|
object DataProviderManager {
|
|
fun registerDataProvider(provider : DataProvider) {
|
|
// ...
|
|
}
|
|
|
|
val allDataProviders : Collection<DataProvider>
|
|
get() = // ...
|
|
}
|
|
{jet}
|
|
This is called an _object declaration_. If there's a name following the *object* keyword, we are not talking about an _expression_ any more. We cannot assign such a thing to a variable, but we can refer to it by its name. Such objects can have supertypes:
|
|
{jet}
|
|
object DefaultListener : MouseAdapter() {
|
|
override fun mouseClicked(e : MouseEvent) {
|
|
// ...
|
|
}
|
|
|
|
override fun mouseEntered(e : MouseEvent) {
|
|
// ...
|
|
}
|
|
}
|
|
{jet}
|
|
|
|
h3. Semantical difference between object expressions and declarations
|
|
|
|
There is one important semantical difference between *object* expressions and *object* declarations:
|
|
* *object* declarations are initialized lazily, when accessed for the first time
|
|
* *object* expressions are executed (and initialized) immediately, where they are used
|
|
|
|
h3. What's next
|
|
|
|
* [Generics] |