在Kotlin中创建抽象类的实例 [英] Create an instance of an abstract class in Kotlin
本文介绍了在Kotlin中创建抽象类的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Kotlin的新手,正在尝试在我的Android项目中使用它.我有以下代码:
I'm new to Kotlin and I'm trying to use it in my Android project. I have this code:
public var oneTouchTimer: CountDownTimer = CountDownTimer(500, 100) {
override fun onTick(l: Long) {
}
override fun onFinish() {
}
}
并抛出错误:
Cannot create an instance of an abstract class.
基本上,我正在尝试创建CountDownTimer的实例,但无法弄清楚如何将其转换为Kotlin.
Basically I'm trying to create an instance of CountDownTimer and cannot figure out how to convert it to Kotlin.
这是Java中的代码:
Here is the code in Java:
CountDownTimer oneTouchTimer = new CountDownTimer(500, 100) {
@Override
public void onTick(long l) {
}
@Override
public void onFinish() {
}
};
推荐答案
您可以使用此方法:
var variableName = object: CountDownTimer(...){
...
}
这篇关于在Kotlin中创建抽象类的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文