Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of Kotlin inheritance

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "Kotlin inheritance case Analysis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

All classes in Kotlin have a public superclass: Any, which is the default parent class for all classes that do not declare a superclass.

Class Example / / implicitly inherits from Any

Java. Lang.Object. In particular, there are no members except for the equals (), hashCode (), and toString () methods.

To explicitly declare a superclass, the syntax is as follows:

Open class Base (p:Int) class Derived (p:Int): Base (p)

If the class has a main constructor, you can initialize the parent class with the parameters of the main constructor (and must).

If the class does not have a primary constructor, each secondary constructor needs to be initialized with the super keyword or delegated to another constructor. It is important to note that in this case, different auxiliary constructors can call different constructors of the base class.

Class MyView:View {constructor (ctx:Context): super (ctx) constructor (ctx:Context,attrs:AttributeSet): super (ctx,attrs)}

The open annotation is the opposite of Java's final: it allows other classes to inherit from that class. By default, all classes in Kotlin are final, that is, they cannot be inherited.

Overwriting method

Kotlin always does something specific, unlike Java,Kotlin, which requires explicit annotations and overrides when it requires overridden methods:

Open class Base {open fun v () {println ("Base.v ()")} fun nv () {println ("Base.nv")}} class Derived (): Base () {override fun v () {println ("Derived.v ()")}}

When overriding the v () of Derived, the ovverride annotation is required, otherwise the compiler will report an error. Without open annotations, such as Base's nv (), the method cannot be overridden in subclasses. In a final class (there is no open comment declaration), open members are prohibited. This means that every member of the final class is also final.

A member marked override is itself open, and subclasses can still override it. If you want to disable overwriting, use final

Open class AnotherDerived (): Base () {final override fun v () {println ("AnotherDerived.v")}}

*, main () verifies polymorphism:

Fun main (args: Array) {var base1: Base = Base () var base2: Base = Derived () var base3: Base = AnotherDerived () base1.v () base3.v ()}

Override attribute

Overriding properties are basically similar to overriding methods; if subclasses want to redeclare properties that have already been declared in the parent class, you need to use override, and the types should be compatible. Each declared property can be overridden by a property with an initializer or a property with a getter method.

Open class Foo {open val x: Int get () {println ("Foo") return 3}} class Bar1: Foo () {overrideval x: Int = 2}

You can use the var property to override the val property, and vice versa. Because the val attribute basically declares a getter method and replaces it with var, and declares a setter method in the derived class.

You can use override to override properties in the main constructor

Interface Aoo {val count: Int} class Aoo1 (overrideval count: Int): Aoo class Aoo2: Aoo {override var count: Int = 0}

Overwriting criterion

In Kotlin, implementation inheritance is governed by the rule that if a class inherits multiple implementations of the same member from its direct superclass, it must override that member and provide its own implementation (possibly using one of them). To represent the supertype of the implementation inherited from it, use the supertype name super qualification in angle brackets, for example, super.

The default method of open class A {open fun f () {println ("A")} fun a () {println ("a")} / / interface is open interface B {fun f () {println ("B")} fun b () {println ("b")}} class C (): a () B {override fun f () {super.f () super.f () println ("C")}}

There is no problem with the above code inheriting from An and B, a () and b () because C knows which class to inherit from. But for f (), we have two inheritances, so we need to override f () and provide our implementation to disambiguate.

This is the end of the content of "Kotlin inheritance case Analysis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report