问题描述
当我注意到RuntimeException
有两个选项时,我正在编写一些代码,并且会抛出RuntimeException
作为默认值,一个选项来自java.lang
,另一个来自kotlin
:
I was writing some code and was going to throw a RuntimeException
as a default for something, when I noticed that there are two options for RuntimeException
- one from java.lang
and one from kotlin
:
在kotlin版本中,还有其他所有以相同方式工作的方法:
Inside the kotlin version of this, there's an entire list of other methods which work the same way :
因此,据我了解,软件包kotlin
中的一个只是Java等效项的别名(如果我输入错了,请更正),这导致我想到了一个问题:
So, from my understanding of this, the one from package kotlin
is simply an alias for the java equivalent (correct me if I'm wrong) which leads me to the question :
拥有此别名文件有什么意义,何时应在标准" java等效项上使用它?这是否可以节省一些进口商品?
what is the point of having this alias file and when should you use it over the "standard" java equivalent ? Does this simply save a few imports ?
推荐答案
使用JDK时,它们映射到JDK类.当使用Kotlin for Javascript时,它们将映射到Kotlin Javascript库中的特定实现.关于actual
和expect
的文档此处.
When using the JDK, these map to JDK classes. When using Kotlin for Javascript, they would map to a specific implementation in the Kotlin Javascript library. Documentation about actual
and expect
here.
要回答您的问题,如果您有可能将代码移植到其他平台上,请始终使用kotlin.
变体.否则,没关系.
To answer your question, if there's a chance of you porting your code to also work on another platform, always use the kotlin.
variant. Otherwise, it doesn't matter.
这篇关于来自Kotlin包的RuntimeException用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!