我刚接触Kotlin,我想要在动态类型语言中实现的非常简单,但是在Kotlin中就不是那么容易了。我有一组接受消息对象的消息处理函数。每个消息类都实现了Message接口。我想将每个消息处理函数映射到一个String键:
interface Message
data class TestMessage1(val body: String): Message
data class TestMessage2(val body: String): Message
fun testMessage1Handler(msg: TestMessage1) { println(msg.body) }
fu
我有一个运行得很好的函数。但我对Kotlin还不熟悉,所以我想要一种更像Kotlin的方式。我的算法是非常标准的。所以我的问题是:有没有一种方法可以只用Kotlin已知函数得到同样的结果,或者更像Kotlin的方法?
fun sumOfdivider(numerToCalculate: Int) : MutableList<Int>{
var CalculationNumer = numerToCalculate
val numberList = ArrayList<Int>()
var helpCounter = 1
while (Calculati
如何将属性getter传递给接受函数类型的函数? 下面是我想要实现的一个示例: class Test {
val test: String
get() = "lol"
fun testFun(func: ()->String) {
// invoke it here
}
fun callTest() {
testFun(test::get)
// error: Type mismatch: inferred type is
// KFunction1&
我被kotlin聪明的演员搞混了。在我看来,if(condition) / else等同于when(condition) true -> /false ->,但是智能转换并不适用于my表达式。
我用的是kotlin 1.5.30
data class Person(val name: String)
data class Address(val id: String, val person: Person?)
fun getPersonFromAddress(address: Address?): String {
return if (address?.person !
我正在开发一个包含一些视图的android库。自然,这些视图可以发出事件。
我有一个名为Listener的接口(仅用于这个问题)。如果我用Java编写了这个库,情况将如下所示:
public interface Listener {
void onEvent();
}
public class SomeView extends FrameLayout {
// Some more functions and implementation details
public void setListener(Listener l) { ... }
}
在Kotlin活动中使用
有人知道为什么对method1的调用不编译,而对method2的调用编译吗?
class MyApp {
interface X {
fun <Q : Any, A : Any> method1(argStr: String = "", argQ: Q, argH: (A) -> Unit)
fun <Q : Any, A : Any> method2(argQ: Q, argStr: String = "", argH: (A) -> Unit)
}
fun t
尝试在IntelliJ中构建Kotlin/Ktor应用程序时,要多次警告窗体
Warning:(276, 6) Kotlin: This class can only be used with the compiler argument '-Xuse-experimental=kotlin.Experimental'
是输出。警告指的是
@UseExperimental(KtorExperimentalLocationsAPI::class)
因此,我希望通过设置-Xuse-experimental=kotlin.Experimental. -> Build ->
对于我的研究,我有一个使用Kotlin和Ktor开发的小API。目标是在启动应用程序时显示JSON信息。当我们的web浏览器中有http://127.0.0.1:8080/course/{id}时,应该会看到这些信息。id是某个课程id,如果id不同于浏览器上显示的错误消息,则id只能是egal到1,2,3。 我今天的代码是 fun main(args: Array<String>): Unit =
io.ktor.server.netty.EngineMain.main(args)
@Suppress("unused") // Referenced in a
背景:我是kotlin的新手,有一些java方面的经验。
问题:--我正在通过kotlin教程来了解集合和使用sort*函数的方法,但我对语法感到困惑。
使用kotlin sortedByDescending函数编写的代码:
// Return a list of customers, sorted in the descending by number of orders they have made
fun Shop.getCustomersSortedByOrders(): List<Customer> =
customers.sortedByDescendi
我正在用Kotlin编写一个应用程序,并希望将位置类型作为字符串存储到SQLite中。地点是可分割的。Parcelable有将字符串序列化的方法吗?
我想做这样的事:
var loc:Location
var str:Sring
... Code to Initialize 'loc'...
str = loc.serilizeToString()