Tag: 句法

Kotlin:访问when语句的参数

有没有办法获得我传入when语句的expression式的值? 在我的应用程序中,我有一个这样的KeyListener _content.addKeyListener(object : KeyAdapter() { override fun keyPressed(e: KeyEvent?) = when(e?.keyCode) { KeyEvent.VK_T -> mainWindow.enterTrainingState() KeyEvent.VK_P -> mainWindow.enterPlayState() KeyEvent.VK_E -> mainWindow.close() else -> println(e?.keyCode) } }) Kotlin有一个整洁的语法来访问e?.keyCode ? 我真的不想重复这个expression。

如何在Kotlin中声明和初始化MutableSet?

如何将MutableSetvariables声明为{1,2,3}?

为什么Kotlin没有实现Int.plus(value:String)?

当你可以这样做的时候会引起不适: val string = ” abc ” val integer = 8 val result = string + integer 并不能做到: val result = integer + string 它有隐藏的意义还是一个遗漏?

在Kotlin中增加可空Int的简单而好的方法

什么是在Kotlin中增加可空Int的最简单和最好的可读方法? 除了这样做还有别的方法吗? var myInt: Int? = 3 myInt = if(myInt!=null) myInt+1 else null 如果myInt是简单的variables,这是相当好的,但是当myInt是更长的expression式时它可以长得很长。

variables名称前的Kotlin星号运算符或Kotlin中的Spread运算符

我想知道在Kotlin中variables名称前面的星号是什么。 我在Spring引导Kotlin例子中看到了这个( *args ): @SpringBootApplication open class Application { @Bean open fun init(repository: CustomerRepository) = CommandLineRunner { repository.save(Customer(“Jack”, “Bauer”)) repository.save(Customer(“Chloe”, “O’Brian”)) repository.save(Customer(“Kim”, “Bauer”)) repository.save(Customer(“David”, “Palmer”)) repository.save(Customer(“Michelle”, “Dessler”)) } } fun main(args: Array) { SpringApplication.run(Application::class.java, *args) }

Kotlin – 的字符是什么?

我一直在使用/学习Kotlin一段时间,我只有一次见过“被使用”。 System. `in` 我试过find这个东西,但是我没有办法。 我希望有人能帮助我。 问候