在Kotlin开发简单的主队列时,我碰到一个没有经过检查的警告,我无法摆脱: private val pq: Array<T> = arrayOfNulls<Comparable<T>>(capacity) as Array<T> 以下是Kotlin优先级队列的完整源代码: class UnorderedMaxPQ<T : Comparable<T>>(capacity: Int) { private val pq: Array<T> = arrayOfNulls<Comparable<T>>(capacity) as Array<T> private var size = 0 fun isEmpty() = size == 0 fun size() = size fun insert(x: T) { pq[size++] = x } fun delMax(): T { var max = 0 […]
我有一个像Java这样的方法: public <T extends A & B> methodName(T arg, …) 其中A是一个类,B是一个接口。 在我的kotlin类中,我有另一个C类型的variable ,我希望实现以下功能: if (variable is A && variable is B) { methodName(variable, …) } else { // do something else } 是否有可能正确地投下variable以便它可以作为参数没有错误? 目前,该variable具有setter方法,所以智能铸造不可用。 但是,我也用一个本地的val测试它,并且这个值被推断为具有类型Any而不起作用。
在Java中,我可以编写如下代码: void cast(A a) { if(a instanceof Person) { Person p = (Person) a; } } 在Kotlin,我该怎么办? as操作员还是操作员?
var a : Double a = Math.sin(10) // error: the integer literal does not conform to the expected type Double a = Math.sin(10.0) //This compiles successfully println(a) kotlin为什么不执行隐式类型转换并强制我们传递确切类型的数据? fun sin(value: Double): Double // at kotlin documentation