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 doSomething
and doAnotherThing
. All methods in the interface are abstract methods by default, so there is no need to use abstract
the 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 MyInterface
the interface and implements two methods: doSomething
and . doAnotherThing
Note that when implementing methods in the interface, you need to use override
the 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 MyClass
and two methods implemented by it are called. Since MyClass
implements MyInterface
the interface, you can use MyClass
instances as MyInterface
types.
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 property
abstract 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 MyInterface
in the interface property
and 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, MyInterface1
and MyInterface2
defines a method respectively, MyInterface3
inherits MyInterface1
and 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 MyInterface3
and 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 doSomething
and 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 MyInterface
the interface. MyClass1
Inherits MyInterface
the default implementation in , which MyClass2
overrides MyInterface
the 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()
}
MyInterface
An interface named and a class that implements the interface are defined here MyInterfaceImpl
. MyInterface
A method named is defined in the companion object of , create
which is used to create MyInterface
the implementation object of the interface. Finally, an instance is created main
through 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, companion
the keyword can be omitted. For example:
interface MyInterface {
object Factory {
fun create(): MyInterface = MyInterfaceImpl()
}
fun doSomething()
}
Here, object
the keyword is used to define a Factory
companion object named to provide MyInterface
methods 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 by
be 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()
}
MyInterface
An 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 MyInterface
the interface and myInterface
delegating the instance as a property MyInterface
. Finally, main
an instance is created in the function MyInterfaceDelegate
and 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()
}
MyInterfaceImpl
Here an instance and an instance are created MyInterfaceDelegate
and their doSomething
methods are called respectively. Since MyInterfaceDelegate
implements MyInterface
the interface and myInterface
delegates the instance as a property, MyInterfaceDelegate
calls to methods on the instance doSomething
will be forwarded to myInterface
the instance's doSomething
method. In addition, since MyInterfaceImpl
implements MyInterface
the interface and overrides the method, when calling the method doSomething
directly on the instance, the method in will be called and will not be proxied to .MyInterfaceImpl
doSomething
MyInterfaceImpl
doSomething
MyInterfaceDelegate