我试图继承以下类型,但编译器说它是最终的.
class Dice(private var side : Int)
{
constructor(D : DiceTypesK) : this(D.value) {}
}
class ExplodedDice(private val D : DiceTypesK) : Dice(D)
// ^^^^ this class is final and
// can not be inherited from
Run Code Online (Sandbox Code Playgroud)
为什么我的类型是最终的,因为我不打算这样做?
我有一个Java HashMap填充为
HashMap<String, Integer> myMMap = new HashMap<String, Integer>();
for (int i = 0; i < objects.size(); ++i) {
myMap.put(objects.get(i), i);
}
Run Code Online (Sandbox Code Playgroud)
而我正试图将其转换为Kotlin.我尝试了下面的方法,但我得到了空值.
var myMap : HashMap<String, Int>? = null
for (i in objects){
//myMap?.put(i, objects.indexOf(i))
myMap?.put("sample", 3)
System.out.println("myMapInForLoop" + myMap)
}
Run Code Online (Sandbox Code Playgroud)
它打印I/System.out: myMapInForLoopnull.
我尝试过使用hashMapOf函数,但它只允许1个值,所以我不能把它放进去myMap.
如何将下面的代码转换为泛型类,以便可以指定类型参数T而不是具体类型User?
class UserService : BaseService {
val query = datastore.createQuery(User::class.java)
}
Run Code Online (Sandbox Code Playgroud) 我在 Java 中有一个类 A,在 Kotlin 中有一个接口 B。
// kotlin
interface B {
fun optional()
}
Run Code Online (Sandbox Code Playgroud)
// java
class A implements B {
}
Run Code Online (Sandbox Code Playgroud)
我想在 Kotlin 接口 (B) 中编写默认方法(Java 8 特性),并想在类 A 中实现它,我该如何实现?
提前致谢。
刚接触 kotlin,有不少问题和解答,大部分是 Java 方面的。在遵循文档并验证大量 SO问题/答案之后,Android Studio 抱怨函数中存在以下问题。函数是
\n\nfun getRandomQuote() {\n val randomValue = Random.nextInt(quotes.size)\n //return quotes[randomValue]\n return quotes.get(randomValue)\n}\nRun Code Online (Sandbox Code Playgroud)\n\n引号是一个 arrayOf(strings)
\n\nval quotes = arrayOf("Alert today \xe2\x80\x93 Alive tomorrow.",...)\nRun Code Online (Sandbox Code Playgroud)\n\n它说对于
\n\nquotes.get(randomValue)\nRun Code Online (Sandbox Code Playgroud)\n\n它应该是一个Unit但发现了String。randomValue 应该是文档使用 nextInt定义的整数,除非我误解了文档。我没有看到这个问题。我只是想从数组中随机返回一个字符串。我认为工作室可能又出现了误报,所以我清理了该项目,但它在构建时停止了。任何人都知道我做错了什么。
\nKotlin无法编译此代码,因为编译器声明"Error:Smart cast to'Nothing'是不可能的,因为'accumulator'是一个复杂的表达式"
叶奥尔德函数被调用你所期望的,即,我希望返回indexOfMax -但为什么"智能投"未能投给什么是理解更重要accumulator的Int
fun indexOfMax(a: IntArray): Int? {
return a.foldIndexed(null) { index, accumulator, element ->
return if (accumulator is Int) {
var i:Int = accumulator
return if (accumulator == null) index
else if (element > a[i]) index
else accumulator
} else accumulator
}
}
Run Code Online (Sandbox Code Playgroud)
是的,接受的答案有效!这是解决方案:
fun indexOfMax(a: IntArray): Int? {
return a.foldIndexed(null as Int?) { index, accumulator, element ->
if (accumulator == null) index
else if (element >= a[accumulator]) index
else accumulator
}
}
Run Code Online (Sandbox Code Playgroud) Java在此方面更为明确,因此初学者更容易理解代码在做什么,但是学习Kotlin知道when时有一个经验法则:指示它返回什么,而何时返回扩展课程?例:
data class SlothGeneric(val slothName: String,
val isTwoFingered: Boolean,
var slothWeight: Int): Mammal(slothName)
Run Code Online (Sandbox Code Playgroud)
我们如何知道该类SlothGeneric是否正在返回一个Mammal类,或者是否正在扩展它?
我的问题可能是菜鸟,但请帮助我。我不明白在Kotlin中不允许使用“ is”关键字的非开放类的目的是什么。
示例代码1
fun main(){
val randomclassobject = RandomClass()
println(randomclassobject is someRandomInterface)
}
open class RandomClass{
}
interface someRandomInterface{
fun mustImplementThis()
}
Run Code Online (Sandbox Code Playgroud)
上面的代码工作得很好
现在示例代码2
fun main(){
val randomclassobject = RandomClass()
println(randomclassobject is someRandomInterface)
}
class RandomClass{
}
interface someRandomInterface{
fun mustImplementThis()
}
Run Code Online (Sandbox Code Playgroud)
如果没有打开关键字,它将显示错误“ Error:(3,34)Kotlin:不兼容的类型:someRandomInterface和RandomClass”
为什么打开关键字真的很重要?
我正在尝试在 kotlin 中使用箭头
箭头具有三个功能
IO {}
IO.fx {}
IO.fx { !effect}
Run Code Online (Sandbox Code Playgroud)
我想知道这两者的区别。我知道 IO.fx 和 IO.fx {!effect} 帮助我们使用副作用,但是两者之间有什么区别,为什么我要使用一个而不是另一个