expressions and declarations of Kotlin programming objects

Source: Internet
Author: User
Tags anonymous inheritance

Kotlin Programming related knowledge points : Kotlin programming using IntelliJ IED and understanding Source files (. kt) Kotlin programming and other properties Kotlin programming methods such as using Kotlin programming Kotlin programming of the parent class and the inherited parent class Kotlin programming interface and implementation interface Kotlin programming associated objects, abstract class, sealed class Kotlin programming nested class, Inner class, Anonymous inner class

Objective:

Sometimes we want to create an object that has little modification to the current class, but do not want to re-declare a subclass. Java solves this problem with the concept of anonymous inner classes. Kotlin implements this conceptual object expression cleverly with object expressions and object declarations

Create some objects that inherit the anonymous class with an expression:

Package com.xingen.kotlin.day2017529 Fun

Main (args:array<string>) {
    test ()
    test1 ()
}/
* *
 * Test anonymous class with parent class and interface */fun
Test () {
    var t = object:test2017529 ("Create an anonymous inner class for inheritance extension"), Test1interface {
        Override Fun Test () {
            println ("Create an anonymous inner class for interface extension")}
    }
    println (T.V)
    t.test ()
}
/**
 * Test anonymous class without inheritance and implementation *
/Fun test1 () {
    var T = object {
        var show:string = "Anonymous class without parent class"
    }
    Prin TLN (t.show)
}
/**
 * Create a parent class
 *
/Open Class Test2017529 (str:string) {public
    open var v = Str
}
/**
 * Defines an interface
 *
/interface Test1interface {fun
    test ()
}

Output Result:

Creating an anonymous inner class that inherits the extension of an
interface extends the anonymous inner class
without the parent class's anonymous class

Know:

How to create an anonymous class var T=object: Parent class (), interface name.
Note : If there is a constructor in the parent class, it is necessary to pass the parameter when the anonymous class has more than one parent class, the parent class is separated by commas to create an anonymous class object declaration that does not inherit the parent class and does not implement the interface.

Declare an object of a singleton class in Kotlin, decorated with object. A singleton pattern that is often used in development.

Package com.xingen.kotlin.day2017529 Fun

Main (args:array<string>) {
    testlistener.test ()
}
/ * * Object
 keyword declares a singleton object
 *
 *  objects declaration differs from object expression:
 *
 *    object declarations and variable declarations cannot be assigned to the left of the equals sign as an rvalue.
 *
 *  call mode:
 *    1. Class name. property name
 *    2. Class name. Method () */
object  testlistener: Test1interface {
    override fun Test () {
      println ("Object declaration class objects")
    }
}

Output Result:

Object Declaration class objects

Note: The object declaration cannot be local (for example, it cannot be declared directly inside the function), but it can be embedded in the declaration of other objects or in a non-intrinsic class within the accompanying object

Package com.xingen.kotlin.day2017529 Fun

Main (args:array<string>) {
    day2017529.test ()
}
Class day2017529{
    /**
     * Companion object declares a companion object, which can be omitted.
     * Methods in the accompanying object can be used as static
     methods
     *  * @JvmStatic tags, real static methods and static properties */
    Companion object test{
       @ Jvmstatic Fun  Test () {
            println ("Static use of adjoint object")
        }
    }
}

Output Result:

Static use of accompanying objects

Companion: The object declaration inside the tag class, that is, the decoration accompanying object, the name of the object can be omitted. Using @jvmstatic annotations, you can use multiple accompanying objects to become real static methods and properties. Object expressions, declaration of objects, and the difference between objects:

The object expression is initialized immediately where it is used, and the execution of the

Object declarations are lazy-loaded, and the accompanying objects that are initialized when the access call is used are the corresponding class-loading, initialized, and static initial counterparts in Java.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.