如何在 Kotlin 中创建抽象类的实例?
kotlin mobile developmentapps/applications
在 Kotlin 中,如果一个类使用 abstract 关键字定义,则该类被称为抽象类。在 Kotlin 中,我们无法创建抽象类的实例。抽象类只能由另一个本质上应该是抽象的类来实现。为了使用抽象类,我们需要创建另一个类并继承该抽象类。
示例 - Kotlin 中的抽象类
以下示例演示了如何在 Kotlin 中创建抽象类的实例。
abstract class myInter {
abstract var absVariable : String
abstract fun absMethod()
}
class myClass : myInter() {
// 我们不能创建抽象类的实例;
// 但是我们可以使用另一个类实现相同的功能
override var absVariable: String = "Test"
override fun absMethod() {
println("Implemented the abstract method")
}
}
fun main(args: Array<String>) {
var obj = myClass()
obj.absMethod()
}
输出
执行时,它将产生以下输出 −
Implemented the abstract method

