kotlin界面
In this tutorial, we’ll be looking into interfaces in Kotlin. Kotlin interface is like contracts that can be used by classes for specific behaviors.
在本教程中,我们将研究Kotlin中的界面。 Kotlin界面就像合同,类可以将其用于特定行为。
(Kotlin Interface)
Interfaces in Kotlin contain the definitions of functions and properties that are abstract.
Kotlin中的接口包含抽象的函数和属性的定义。
Interfaces can have the implementation of the functions.
接口可以具有功能的实现。
Also, interfaces can have non-abstract properties by defining their getters and setters.
另外,通过定义接口的getter和setter方法,接口可以具有非抽象属性。
But still, an interface can’t store the state. Interfaces are useless unless they’re implemented by a class.
但是,接口仍然无法存储状态。 除非接口由类实现,否则它们是无用的。
An interface can’t have any constructor.
接口不能有任何构造函数 。
(Defining an Interface)
An interface in Kotlin can be defined using the keyword interface
.
可以使用关键字interface
定义Kotlin中的interface
。
interface First{
fun stringEcho() : String //abstract function that returns a string
fun printMe(){
println("This function has a body containing the implementation")
}
var str : String //abstract property
val i : Int //abstract property
}
The printMe()
function is non-abstract since it contains an implementation.
printMe()
函数是非抽象函数,因为它包含一个实现。
Additionally, we can also declare the property, provided we set accessor methods to it.
此外,我们还可以声明该属性,只要我们为其设置访问器方法。
interface Second {
val i: Int
get() = 10
var str: String
get() = "You Name"
set(value) {}
}
We cannot use the backing field
from Properties in interfaces since an interface doesn’t store any state and hence no previous values.
我们不能在接口中使用“ 属性”中的后备field
,因为接口不存储任何状态,因此不存储任何先前的值。
(Implementing Interfaces)
The above interfaces that we’ve defined can be implemented over Kotlin Class as shown below.
我们定义的以上接口可以在Kotlin类上实现,如下所示。
class A : First {
override var str: String = "str"
get() = "Value is $field"
override val i: Int = 1
override fun stringEcho(): String {
return "Hello"
}
}
We need to implement all the abstract properties of the interface or else set the class as an abstract class
.
我们需要实现接口的所有抽象属性,或者将类设置为abstract class
。
An override
modifier overrides the property/function from the interface defined.
override
修饰符从定义的接口覆盖属性/功能。
Implementation of non-abstract properties/functions is not necessary.
非抽象属性/功能的实现不是必需的。
Let’s initialize the class in the main function.
让我们在main函数中初始化该类。
fun main(args: Array<String>) {
var a = A()
a.printMe()
a.str = "Kotlin Interfaces"
println(a.str)
println(a.stringEcho())
}
(Overriding properties inside the constructor)
Properties can be overriden as constructor parameters of the class too.
属性也可以作为类的构造函数参数覆盖。
class B(override var str: String, override val i: Int) : First{
override fun stringEcho(): String {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
(Implementing Multiple Interfaces)
A class can implement multiple interfaces too using the above concept.
Let’s look at an example wherein both the interfaces have the same functions defined as shown below.
使用上述概念,一个类也可以实现多个接口。
让我们看一个示例,其中两个接口具有如下所示定义的相同功能。
interface Formal {
fun echoName(username: String): String {
return "Mr. $username"
}
}
interface Informal {
fun echoName(username: String): String {
return username
}
}
In this case, if at all we override the function implementation in our class, our class would have a single common overridden implementation as shown below.
在这种情况下,如果我们完全重写了类中的函数实现,则我们的类将具有单个公共的重写实现,如下所示。
class D : Formal, Informal {
override fun echoName(username: String): String {
return username
}
}
What if we need to access the function from the interface?
如果我们需要从界面访问该功能怎么办?
We use the keyword super
.
我们使用关键字super
。
Now in the above code, using super
would give a compile time error since the compiler doesn’t know which of the interfaces to goto.
现在在上面的代码中,使用super
将产生编译时错误,因为编译器不知道要转到哪个接口。
So in such cases in Kotlin, we need to specify the interface along with super
as shown below.
因此,在Kotlin中,在这种情况下,我们需要指定接口以及super
,如下所示。
class D : Formal, Informal {
override fun echoName(username: String): String {
return super<Formal>.echoName(username)
}
}
fun main(args: Array<String>) {
val d = D()
println(d.echoName("Jake Wharton"))
}
//Prints
//Mr. Jake Wharton
Another example:
另一个例子:
override fun echoName(username: String): String {
return "Formal : ${super<Formal>.echoName(username)}. Informal : ${super<Informal>.echoName(username)}"
}
That’s all for kotlin interface tutorial.
这就是kotlin界面教程的全部内容。
Reference: Official Documentation
参考: 官方文档
kotlin界面