Kotlin Object expressions and object declarations
Kotlin uses object expressions and object declarations to implement an object that creates a class that makes minor changes to a class, and does not need to declare a new subclass. an object expression
An object that implements an anonymous inner class through an object expression is used in the parameters of the method:
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 = +
}
If the superclass has a constructor, you must pass the parameter to it. Multiple hyper-types and interfaces can be separated by commas.
Object expressions allow you to get an object directly over the definition of a class:
Fun Main (args:array<string>) {
val site = object {
var name:string = "Rookie Tutorial"
var url:string = "Www.run Oob.com "
}
println (site.name)
println (site.url)
}
Note that anonymous objects can be used as types that are declared only on local and private scopes. If you use an anonymous object as the return type of a public function or as a type of a public property, the actual type of the function or property is the super-type of the anonymous object declaration, or any if you do not declare any of the superclass types. Members added in the anonymous object will not be accessible.
Class C {
//private function, so its return type is 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: Failed to resolve reference "X"
}
}
Other variables in the scope can be easily accessed in object representations:
Fun Countclicks (window:jcomponent) {
var clickcount = 0
var entercount = 0
Window.addmouselistener (object: Mouseadapter () {
override mouseclicked (e:mouseevent) {
clickcount++
} override fun
mouseentered ( e:mouseevent) {
entercount++
}
})
//...
}
Object Declaration
Kotlin uses the OBJECT keyword to declare an object.
In Kotlin we can easily get a singleton by object declaration.
Object Dataprovidermanager {fun
registerdataprovider (provider:dataprovider) {
//...
}
Val alldataproviders:collection<dataprovider>
Get () =//...
}
Referencing the object, we can use its name directly:
Dataprovidermanager.registerdataprovider (...)
Of course you can also define a variable to get the object, when you define two different variables to get the object, you will find that you can not get two different variables. That is to say, in this way, we get a single case.
var data1 = Dataprovidermanager
var data2 = dataprovidermanager
data1.name = "Test"
print ("Data1 name = ${dat A2.name} ")
Example
In the following instance, two objects all output the same URL address:
Object Site {
var url:string = ""
val name:string = "Rookie Tutorial"
} fun
Main (args:array<string>) {
VA R S1 = site
var s2 = site
s1.url = "www.runoob.com"
println (S1.url)
println (s2.url)
}
The output is:
Www.runoob.com
www.runoob.com
objects can have super types:
Object Defaultlistener:mouseadapter () {
override fun mouseclicked (e:mouseevent) {
//...
}
Override Fun Mouseentered (e:mouseevent) {
//...
}
}
Unlike object expressions, when an object is declared inside another class, the object cannot be accessed through an instance of an external class, but only through the class name, and the object cannot directly access methods and variables of the outer class.
Class Site {
var name = "Rookie Tutorial"
object desktop{
var url = "www.runoob.com" Fun
showname () {
print{" Desk legs $name "}//Error, cannot access methods and variables of external class}}} Fun
Main (args:array<string>) {
var site = Site ()
site. Desktop.url//error, cannot access the object through an instance of an external class
Site.DeskTop.url//Correct
}
Associated Objects
An object declaration inside a class can be tagged with the companion keyword so that it is associated with an external class, and we can access the inner elements of an object directly through an external class.
Class MyClass {
Companion object Factory {fun
Create (): MyClass = MyClass ()
}
}
val instance = Myclas S.create () //access to the inner element of the object
We can omit the object name from the object, and then use Companion instead of the object name to declare:
Class MyClass {
companion object {
}
}
val x = myclass.companion
Note: only one inner associative object can be declared within a class, that is, the keyword companion can only be used once.
Make members of the associated object look like static members of other languages, but at run time they are still instance members of the real object. For example, you can also implement an interface:
Interface Factory<t> {fun
Create (): T
}
class MyClass {
companion object:factory< myclass> {
override fun Create (): MyClass = MyClass ()
}
}
semantic differences between an object expression and an object declaration
There is an important semantic difference between an object expression and an object declaration:
Object expressions are executed immediately where they are used.
Object declarations are deferred for the first time they are accessed.
The initialization of the associated object is matched to the semantics of the Java static initializer when the corresponding class is loaded (parsed)
Transferred from: http://www.runoob.com/kotlin/kotlin-object-declarations.html