是否可以这样做:

/**
 * Converts all of the characters in the string to upper case.
 *
 * @param str the string to be converted to uppercase
 * @return the string converted to uppercase or empty string if the input was null
 */
fun String?.toUpperCase(): String = this?.toUpperCase() ?: ""
  • 这会怎么样?这将使toUpperCase个零安全.
  • 我有什么问题?返回值为this?.toUpperCase()

Is the only option to rename my extension function or is there a way to refer to the "super" function from within it?

推荐答案

不能重写现有成员函数.

If a class has a member function, and an extension function is defined which has the same receiver type, the same name is applicable to given arguments, the member always wins.

source

重命名我的扩展函数是唯一的 Select ,还是有办法从它内部引用"超级"函数?

您必须重命名扩展函数,并从内部调用要使用的成员函数.

Kotlin相关问答推荐

直接从SON解析NonEmptyList

如何进行基于lambda/谓词的两个列表的交集?

如何通过更改现有数据类型来执行Android房间数据库迁移?

init中的NPE抽象函数变量

Kotlin 中的密封和内部有什么区别?

在 Kotlin 中,我可以在集合上有一个条件构建器元素吗?

如何将glide显示的图像下载到设备存储中.Kotlin

将子元素放在一个列表中

TestContainers PostgreSQLContainer 与 Kotlin 单元测试:Not enough information to infer type variable SELF

在 APK META-INF/library_release.kotlin_module 中复制的重复文件

有没有办法重用 Job 实例?

如何在 android jetpack compose 中相互重叠列表项?

Kotlin 创建snackbar

Firebase 权限被拒绝

当被Spring代理类访问时,Kotlin实例变量为null

如何在kotlin语言中将字符转换为ascii值

如何在Kotlin中使用ViewModelProviders

Kotlin中保留的关键字是什么?

不推荐使用仅限生命周期的LifecycleEvent

Kotlin:获取文件的扩展名,例如.txt