gpt4 book ai didi

function - 确保kotlin方法是静态的,顶级的或带注释的@JvmStatic

转载 作者:行者123 更新时间:2023-12-02 13:24:09 25 4
gpt4 key购买 nike

如何将main声明为静态,以便该方法将按以下方式运行(交互式):

thufir@dur:~/kotlin$ 
thufir@dur:~/kotlin$ kotlinc
Welcome to Kotlin version 1.1.51 (JRE 9.0.0.15+181)
Type :help for help, :quit for quit
>>>
>>> println("hello world");
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.intellij.util.text.StringFactory to constructor java.lang.String(char[],boolean)
WARNING: Please consider reporting this to the maintainers of com.intellij.util.text.StringFactory
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
hello world
>>>
>>> :quit
thufir@dur:~/kotlin$

编译:
thufir@dur:~/kotlin$ 
thufir@dur:~/kotlin$ kotlinc Hello.kt
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.intellij.util.text.StringFactory to constructor java.lang.String(char[],boolean)
WARNING: Please consider reporting this to the maintainers of com.intellij.util.text.StringFactory
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
thufir@dur:~/kotlin$
thufir@dur:~/kotlin$ kotlin HelloKt
error: could not find or load main class HelloKt
thufir@dur:~/kotlin$
thufir@dur:~/kotlin$ kotlin Hello
error: 'main' method of class Hello is not static. Please ensure that 'main' is either a top level Kotlin function, a member function annotated with @JvmStatic, or a static Java method
thufir@dur:~/kotlin$
thufir@dur:~/kotlin$ cat Hello.kt
class Hello {


public fun main(args: Array<String>) {
println("Hello, world!" + args[0])
}
}
thufir@dur:~/kotlin$

也可以看看:

https://kotlinlang.org/docs/tutorials/command-line.html

最佳答案

在本教程中,该方法在顶层声明,而不是在class Hello内部声明。或者,您可以编写

import kotlin.jvm.JvmStatic

object Hello {
@JvmStatic
public fun main(args: Array<String>) {
println("Hello, world!" + args[0])
}
}

关于function - 确保kotlin方法是静态的,顶级的或带注释的@JvmStatic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46978386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com