Kotlin Tutorial Learning-Companion object, Getter,setter, interior, local, anonymous class, mutable parameter

Source: Internet
Author: User

The Magical Wuhan University International Software College, only freshman will attend summer training, now sophomore, examination to 7.9, more than 20 days of the end of the training, and then a few days, but also early to school, poor my summer vacation so no.

Then say Kotlin:

First look at a class named message

In this class there is a section of code contained in the companion object, it should be said that the Kotlin class does not support the static variable, so you need to use companion object to declare the static variable, In fact, this platformstatic variable is not really a static variable, but a companion object,

The associated object is in an inner class defined in the message class called Companion:


It is common to see the compiler automatically generating classes in Kotlin, so what does this companion object understand?

My understanding is that the associated object is associated with a singleton pattern, which is an object of the companion class, which accompanies the message class.

Companion this inner class is generated automatically by the compiler, and we can define our own inner classes, such as:


Back in the message class, we can see that there are also get and set under the info and what fields, well, this is the getter and setter functions, the general Getter function using the default is good, the setter function is actually at the time of assignment to do some processing, To prevent the setter function from infinitely recursive calls, you can use the $ symbol to access member variables in a class, bypassing getter and setter functions to directly access member variables.

Of course, in Kotlin in addition to the inner class, there are anonymous classes and class,:

Anonymous class:

You can see that r this variable belongs to a type that is anonymous (no name provided)

Local class:


The partial class, which is defined within the function, is accessible only within the function.

Some additional features, such as variable parameters, are also available in the Kotlin.

The result of this code operation is as follows:

Let's test the message class:

Visible Kotlin's getter and setter play a role

Kotlin Tutorial Learning-Companion object, Getter,setter, interior, local, anonymous class, mutable parameter

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.