[英]How to create an instance of anonymous class of abstract class in Kotlin?
[英]Cannot create an instance of an abstract class Random kotlin
我试图在Android Studio上制作Dice Roller应用程序,但是当我将此Random()放置时,出现了一个错误,说“无法创建抽象类的实例”,请帮帮我
package com.example.diceroller
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rollbutton: Button = findViewById(R.id.roll_button)
rollbutton.setOnClickListener(
View.OnClickListener { diceRoll() }
)
}
private fun diceRoll() {
val resultText: TextView = findViewById(R.id.result_text)
val randomInt = Random().nextInt(6) + 1
resultText.text = randomInt.toString()
}
}
我希望每当我单击按钮时输出在6到1之间是随机的
Kotlin中的Random是抽象的,但是它提供了一些可以直接使用的静态方法,例如:
Random.nextInt(6)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.