Kotlin 简明教程
Kotlin - Interface
在本章中,我们将学习 Kotlin 中的接口。在 Kotlin 中,接口与 Java 8 的工作完全类似,这意味着它们可以包含方法实现以及抽象方法声明。接口可以通过一个类来实现,以便使用其定义的功能。我们已经在第 6 章“匿名内部类”部分中引入了一个带有接口的示例。本章将进一步学习它。关键字“interface”用于在 Kotlin 中定义一个接口,如下面的代码片段所示。
In this chapter, we will learn about the interface in Kotlin. In Kotlin, the interface works exactly similar to Java 8, which means they can contain method implementation as well as abstract methods declaration. An interface can be implemented by a class in order to use its defined functionality. We have already introduced an example with an interface in Chapter 6 - section “anonymous inner class”. In this chapter, we will learn more about it. The keyword “interface” is used to define an interface in Kotlin as shown in the following piece of code.
interface ExampleInterface {
var myVar: String // abstract property
fun absMethod() // abstract method
fun sayHello() = "Hello there" // method with default implementation
}
在上面的示例中,我们创建了一个名为“ExampleInterface”的接口,其中我们有几个抽象属性和方法。看看名为“sayHello()”的方法,这是一个已实现的方法。
In the above example, we have created one interface named as “ExampleInterface” and inside that we have a couple of abstract properties and methods all together. Look at the function named “sayHello()”, which is an implemented method.
在下面的示例中,我们将在类中实现上面的接口。
In the following example, we will be implementing the above interface in a class.
interface ExampleInterface {
var myVar: Int // abstract property
fun absMethod():String // abstract method
fun hello() {
println("Hello there, Welcome to TutorialsPoint.Com!")
}
}
class InterfaceImp : ExampleInterface {
override var myVar: Int = 25
override fun absMethod() = "Happy Learning "
}
fun main(args: Array<String>) {
val obj = InterfaceImp()
println("My Variable Value is = ${obj.myVar}")
print("Calling hello(): ")
obj.hello()
print("Message from the Website-- ")
println(obj.absMethod())
}
以上代码段将在浏览器中产生以下输出。
The above piece of code will yield the following output in the browser.
My Variable Value is = 25
Calling hello(): Hello there, Welcome to TutorialsPoint.Com!
Message from the Website-- Happy Learning
如前所述,Kotlin 不支持多重继承,但是通过一次实现两个以上的接口可以实现相同的事情。
As mentioned earlier, Kotlin doesn’t support multiple inheritances, however, the same thing can be achieved by implementing more than two interfaces at a time.
在以下示例中,我们将创建两个接口,稍后我们将在类中实现这两个接口。
In the following example, we will create two interfaces and later we will implement both the interfaces into a class.
interface A {
fun printMe() {
println(" method of interface A")
}
}
interface B {
fun printMeToo() {
println("I am another Method from interface B")
}
}
// implements two interfaces A and B
class multipleInterfaceExample: A, B
fun main(args: Array<String>) {
val obj = multipleInterfaceExample()
obj.printMe()
obj.printMeToo()
}
在上面的示例中,我们创建了两个示例接口 A,B,在名为“multipleInterfaceExample”的类中,我们实现了前面声明的两个接口。以上代码段将在浏览器中产生以下输出。
In the above example, we have created two sample interfaces A, B and in the class named “multipleInterfaceExample” we have implemented two interfaces declared earlier. The above piece of code will yield the following output in the browser.
method of interface A
I am another Method from interface B