【发布时间】:2018-10-02 13:48:27
【问题描述】:
是否可以这样做:
/**
* 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() ?: ""
- 这会做什么?这将使
toUpperCasenull 安全。 - 我有什么问题?返回值,
this?.toUpperCase(), 指扩展功能
是重命名我的扩展函数的唯一选择,还是有办法从其中引用“超级”函数?
【问题讨论】:
-
假设有一种方法可以实现这一点,那将是模棱两可的,对吧?如果我这样做
"foo".toUpperCase()会调用哪个方法? -
我会假设扩展功能....但直到现在我还没有考虑过
-
我的意思是,
toUpperCase已经是 Kotlin 中的扩展方法了。