cool hit counter Kotlin Learning (IV) - classes and objects, inheritance, overrides, abstract classes, properties and fields, interfaces, visibility modifiers, extensions_Intefrankly

Kotlin Learning (IV) - classes and objects, inheritance, overrides, abstract classes, properties and fields, interfaces, visibility modifiers, extensions


Author. Liu someone programmer

statement : Do not reproduce without the permission of the original author

I. Classes and objects

1. kind

Kotlin and java class declarations are the same, represented by class,for example

If it's an empty class, the curly brackets can be omitted

2.constructor (computing)

A primary constructor and multiple secondary functions, with the primary function following the class name, as in

The constructor keyword can be omitted if the main constructor doesn't have any annotations or a configurable modifier.

And the main constructor does not allow any code, the initialization code can be placed in the init block

Let's look at the next constructor

Classes can also declare sub-constructors with a constructor prefix:

3.Instantiating class objects

There is no new in Kotlin, so a direct call to

II. succeed

All classes have a superclass Any, which is somewhat similar to Object in JAVA, but they are different, only similar in concept, because Any has only a few countable methods

Here we define a base class BaseClass

The word open here means public, which is the exact opposite of final in JAVA

Then we define another class to inherit from the base class

Of course, the brackets are all removable

If the class does not have a primary constructor, then each sub-constructor must initialize its base type with the super keyword, as we do in Android using the

III. override

This is well understood, there is inheritance to override, let's look at a piece of code

Here is a Base modified by open, which has a Base method, which prints, I now have a Son class that inherits Base, and override modifies the override v method, and then prints, then my main method, which is called after instantiating this object, prints the Son... finish

A member marked as override is inherently open, meaning that it can be overridden in a subclass, and if you don't want it to be overridden again, you can modify it with another final

What was mentioned above is method override, we can also override properties, but it's the same and requires an override modifier, like

In this code, we can see that a property a is defined in the base class, then an a is overridden in the subclass, and the output is the a value of the subclass

If we want to use the methods of the parent class, we can use super

If you are smashing the internal class you want to use, Then it needs to besuper@Son.v()

In Kotlin, implementation inheritance is governed by the rule that if a class inherits multiple implementations of the same member from its immediate superclass, it must override that member and provide its own implementation (perhaps using one of the inherited ones). To indicate the implementation from which supertype inheritance is used, we use super qualified by the supertype name in pointed brackets, e.g., super.

IV. abstract category

Here open can be undeclared, and we can have an abstract member overriding a non-abstract open member, as in the example

V. Properties and fields

I believe that you should be familiar with the properties, and we have already talked about them in the second

If you want to call it

In kt, get and set are a bit more interesting, and the correct syntax is

The following code

I then defined the get and set of a

VI. interface

The keyword interface for kt's interface, such as

If you want to implement an interface, you can

You can also define some properties in the interface, but they must be abstract or provide access to the outside world

VII. visibility modifier

There are four visibility modifiers in kt, private, protected, internal and public, the default being public

public Everywhere

private Declare that he is visible in the file

internal Same module visible

protected Does not apply to top-level declarations

Another thing to note here is that in JAVA variables modified in our private can eventually be accessed by set/get, but in kt, external classes cannot access private members modified by internal classes

VIII. extensions

Extend, extending a class or object with new functionality without inheriting the class or using any type of design pattern like a decorator

To declare an extension function, we need a receiver type, which is the type being extended, as his prefix, look at the code.

It's clear to see here that a List didn't have an open method, so I added it by extending it and internally implementing it to output the result I wanted to add

Of course, we're not going to be limited to which types, so go through the generic type to decorate a bit

If interested, come to Github and get involved

Kotlin:https://github.com/LiuGuiLinAndroid/Kotlin

Articles in the same series

One destiny, two fortunes, three feng shui, four cultivation of yin virtue and five reading!

Liu Guilin

Micro Signal : Android_LiuGuiLin

Sina Weibo:@ Liu someone programmer


Recommended>>
1、Why is HashMap threadunsafe
2、Javascript arrays
3、How to learn CC plus plus systematically C plus plus game programming career skills learning sheet
4、Tongxiao ConsultingDomestic game filing temporarily closed strict control of the game live web application may not be far
5、ReactNative Common Commands

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号