kotlin-interface

1.Easy to use

In Kotlin, an interface is a type that defines a set of abstract methods (without implementation bodies) that can be implemented by other classes. Interfaces are one of the important means to achieve polymorphism in Kotlin.

Here is a simple interface definition example:

interface MyInterface {
    fun doSomething()
    fun doAnotherThing()
}

An interface named is defined here MyInterface, which contains two abstract methods doSomethingand doAnotherThing. All methods in the interface are abstract methods by default, so there is no need to use abstractthe keyword to modify them.

To implement an interface, you can use the syntax in the class declaration : 接口名. For example:

class MyClass : MyInterface {
    override fun doSomething() {
        println("Do something")
    }

    override fun doAnotherThing() {
        println("Do another thing")
    }
}

A class named is defined here MyClass, which implements MyInterfacethe interface and implements two methods: doSomethingand . doAnotherThingNote that when implementing methods in the interface, you need to use overridethe keyword for modification.

The class can be used with the following code MyClass:

fun main() {
    val obj = MyClass()
    obj.doSomething()
    obj.doAnotherThing()
}

Output:

Do something
Do another thing

Here an instance of is created MyClassand two methods implemented by it are called. Since MyClassimplements MyInterfacethe interface, you can use MyClassinstances as MyInterfacetypes.

2. Properties in the interface

In addition to methods, properties can also be defined in interfaces, which are abstract by default. For example:

interface MyInterface {
    val property: Int
}

An interface named is defined here MyInterface, which contains an propertyabstract property named .

To implement a property in an interface, you can provide a property implementation in the implementation class. For example:

class MyClass : MyInterface {
    override val property: Int
        get() = 123
}

This implements the property MyInterfacein the interface propertyand implements it as a read-only property with a value of 123.

3. Interface inheritance

In Kotlin, an interface can inherit one or more other interfaces. For example:

interface MyInterface1 {
    fun doSomething()
}

interface MyInterface2 {
    fun doAnotherThing()
}

interface MyInterface3 : MyInterface1, MyInterface2 {
    fun doMoreThing()
}

Three interfaces are defined here, MyInterface1and MyInterface2defines a method respectively, MyInterface3inherits MyInterface1and MyInterface2, and adds a new method.

To implement an interface that inherits other interfaces, you can implement all methods defined by its parent interface and provide all inherited properties in the implementation class. For example:

class MyClass : MyInterface3 {
    override fun doSomething() {
        println("Do something")
    }

    override fun doAnotherThing() {
        println("Do another thing")
    }

    override fun doMoreThing() {
        println("Do more thing")
    }
}

The interface is implemented here MyInterface3and all its inherited methods are implemented.

4. Default methods in interfaces

In Kotlin 1.4 and later versions, default implemented methods can be defined in interfaces. For example:

interface MyInterface {
    fun doSomething()

    fun doAnotherThing() {
        println("Do another thing")
    }
}

An interface named is defined here MyInterface, which contains an abstract method doSomethingand a default implementation method doAnotherThing.

To implement a default method in an interface, you can choose to override the method or directly inherit the default implementation. For example:

class MyClass1 : MyInterface {
    override fun doSomething() {
        println("Do something")
    }

    // 继承 MyInterface 中的默认实现
}

class MyClass2 : MyInterface {
    override fun doSomething() {
        println("Do something")
    }

    override fun doAnotherThing() {
        println("Do another thing in MyClass2")
    }
}

Here are two classes that implement MyInterfacethe interface. MyClass1Inherits MyInterfacethe default implementation in , which MyClass2overrides MyInterfacethe default implementation in .

5. Companion objects in interfaces

In Kotlin, an interface can contain companion objects. Similar to the companion object of a class, the companion object of an interface is a singleton instance of the interface. Properties, methods, extension functions, etc. can be defined in companion objects. In addition, companion objects of an interface can also implement the interface itself.

A common use is to provide factory methods through companion objects. For example:

interface MyInterface {
    companion object {
        fun create(): MyInterface = MyInterfaceImpl()
    }

    fun doSomething()
}

class MyInterfaceImpl : MyInterface {
    override fun doSomething() {
        println("Doing something")
    }
}

fun main() {
    val myInterface = MyInterface.create()
    myInterface.doSomething()
}

MyInterfaceAn interface named and a class that implements the interface are defined here MyInterfaceImpl. MyInterfaceA method named is defined in the companion object of , createwhich is used to create MyInterfacethe implementation object of the interface. Finally, an instance is created mainthrough the method in the function and the methods in it are called.MyInterface.create()MyInterface

It should be noted that when defining companion objects in the interface, companionthe keyword can be omitted. For example:

interface MyInterface {
    object Factory {
        fun create(): MyInterface = MyInterfaceImpl()
    }

    fun doSomething()
}

Here, objectthe keyword is used to define a Factorycompanion object named to provide MyInterfacemethods for creating instances. You can call the method directly when using it MyInterface.Factory.create().

6. Interface proxy

In Kotlin, interface proxy is a way to delegate interface instances as attributes, which implements method call forwarding of the interface. Interface proxies are often used to implement some methods in multiple interfaces, or to add or modify some behaviors based on existing interface implementations.

Interface proxies can bybe implemented using the keyword, for example:

interface MyInterface {
    fun doSomething()
}

class MyInterfaceImpl : MyInterface {
    override fun doSomething() {
        println("Doing something")
    }
}

class MyInterfaceDelegate(private val myInterface: MyInterface) : MyInterface by myInterface {
    override fun doSomething() {
        println("Doing something else")
    }
}

fun main() {
    val myInterfaceImpl = MyInterfaceImpl()
    val myInterfaceDelegate = MyInterfaceDelegate(myInterfaceImpl)

    myInterfaceDelegate.doSomething()
}

MyInterfaceAn interface named and a class that implements the interface are defined here MyInterfaceImpl. Then a class named is defined MyInterfaceDelegate, which implements the proxy for the interface by implementing MyInterfacethe interface and myInterfacedelegating the instance as a property MyInterface. Finally, mainan instance is created in the function MyInterfaceDelegateand the methods in it are called.

It should be noted that the method call forwarding implemented through the interface proxy can not only directly implement the call to the method of the proxy interface in the proxy class, but also can override the proxy method in the proxy interface instance and then call it through the proxy class. this method. For example:

interface MyInterface {
    fun doSomething()
}

class MyInterfaceImpl : MyInterface {
    override fun doSomething() {
        println("Doing something")
    }
}

class MyInterfaceDelegate(private val myInterface: MyInterface) : MyInterface by myInterface {
    override fun doSomething() {
        println("Doing something else")
    }
}

fun main() {
    val myInterfaceImpl = MyInterfaceImpl()
    val myInterfaceDelegate = MyInterfaceDelegate(myInterfaceImpl)

    myInterfaceImpl.doSomething()
    myInterfaceDelegate.doSomething()
}

MyInterfaceImplHere an instance and an instance are created MyInterfaceDelegateand their doSomethingmethods are called respectively. Since MyInterfaceDelegateimplements MyInterfacethe interface and myInterfacedelegates the instance as a property, MyInterfaceDelegatecalls to methods on the instance doSomethingwill be forwarded to myInterfacethe instance's doSomethingmethod. In addition, since MyInterfaceImplimplements MyInterfacethe interface and overrides the method, when calling the method doSomethingdirectly on the instance, the method in will be called and will not be proxied to .MyInterfaceImpldoSomethingMyInterfaceImpldoSomethingMyInterfaceDelegate

Guess you like

Origin blog.csdn.net/qq_55888300/article/details/130502640