Kotlin Object Expressions and Declarations
Kotlin uses object expressions and declarations to create objects of a class that makes minor modifications to a certain class, without having to declare a new subclass.
Object Expressions
Object expressions are used to implement an anonymous inner class object for method parameters:
window.addMouseListener(object : MouseAdapter() {
override fun mouseClicked(e: MouseEvent) {
// ...
}
override fun mouseEntered(e: MouseEvent) {
// ...
}
})
Objects can inherit from a base class or implement other interfaces:
open class A(x: Int) {
public open val y: Int = x
}
interface B {……}
val ab: A = object : A(1), B {
override val y = 15
}
If the supertype has a constructor, parameters must be passed to it. Multiple supertypes and interfaces can be separated by commas.
Object expressions can bypass the class definition and directly obtain an object:
fun main(args: Array<String>) {
val site = object {
var name: String = "tutorialpro.org"
var url: String = "www.tutorialpro.org"
}
println(site.name)
println(site.url)
}
Note that anonymous objects can be used as types declared only in local and private scopes. If you use an anonymous object as the return type of a public function or as the type of a public property, the actual type of the function or property will be the supertype declared in the anonymous object declaration. If you do not declare any supertype, it will be Any. Members added in the anonymous object will not be accessible.
class C {
// Private function, so its return type is the anonymous object type
private fun foo() = object {
val x: String = "x"
}
// Public function, so its return type is Any
fun publicFoo() = object {
val x: String = "x"
}
fun bar() {
val x1 = foo().x // No problem
val x2 = publicFoo().x // Error: unresolved reference “x”
}
}
In object expressions, it is convenient to access other variables in the scope:
fun countClicks(window: JComponent) {
var clickCount = 0
var enterCount = 0
window.addMouseListener(object : MouseAdapter() {
override fun mouseClicked(e: MouseEvent) {
clickCount++
}
override fun mouseEntered(e: MouseEvent) {
enterCount++
}
})
// …
}
Object Declarations
Kotlin uses the object
keyword to declare an object.
In Kotlin, we can conveniently obtain a singleton through object declarations.
object DataProviderManager {
fun registerDataProvider(provider: DataProvider) {
// …
}
val allDataProviders: Collection<DataProvider>
get() = // …
}
To reference this object, we simply use its name:
DataProviderManager.registerDataProvider(……)
Of course, you can also define a variable to get this object, but when you define two different variables to get this object, you will find that you cannot get two different variables. In other words, through this method, we get a singleton.
var data1 = DataProviderManager
var data2 = DataProviderManager
data1.name = "test"
print("data1 name = ${data2.name}")
Example
In the following example, both objects output the same URL address:
object Site {
var url:String = ""
val name: String = "tutorialpro.org"
}
fun main(args: Array<String>) {
var s1 = Site
var s2 = Site
s1.url = "www.tutorialpro.org"
println(s1.url)
println(s2.url)
}
The output result is:
www.tutorialpro.org
www.tutorialpro.org
Objects can have supertypes:
object DefaultListener : MouseAdapter() {
override fun mouseClicked(e: MouseEvent) {
// …
}
override fun mouseEntered(e: MouseEvent) {
// …
}
}
Unlike object expressions, when an object declaration is inside another class, this object cannot be accessed through an instance of the external class, and can only be accessed through the class name, and the object cannot directly access the methods and variables of the external class.
```kotlin class Site { var name = "tutorialpro.org" object DeskTop{ var url = "www.tutorialpro.org" fun showName(){ print{"desk legs $name"} // Error, cannot access methods and variables of