我正在尝试Kotlin的一些基础知识->
程序:
fun createDate(day: Int, month: Int, year: Int, hour: Int = 0, minute: Int = 0, second: Int = 0) {
print("TEST", "$day-$month-$year $hour:$minute:$second")
}
createDate(1,7,1997)
错误:
error: none of the following functions can be called with the arguments supplied:
@InlineOnly public inline fun print(message: Any?): Unit defined in kotlin.io
@InlineOnly public inline fun print(message: Boolean): Unit defined in kotlin.io
@InlineOnly public inline fun print(message: Byte): Unit defined in kotlin.io
@InlineOnly public inline fun print(message: Char): Unit defined in kotlin.io
@InlineOnly public inline fun print(message: CharArray): Unit defined in kotlin.io
@InlineOnly public inline fun print(message: Double): Unit defined in kotlin.io
@InlineOnly public inline fun print(message: Float): Unit defined in kotlin.io
@InlineOnly public inline fun print(message: Int): Unit defined in kotlin.io
@InlineOnly public inline fun print(message: Long): Unit defined in kotlin.io
@InlineOnly public inline fun print(message: Short): Unit defined in kotlin.io
print("TEST", "$day-$month-$year $hour:$minute:$second")
不知道我在做什么错,我一直在关注-> https://www.toptal.com/software/kotlin-android-language
答案 0 :(得分:4)
应该是这样,因为kotlin的默认打印功能只有一个参数
fun createDate(day: Int, month: Int, year: Int, hour: Int = 0, minute: Int = 0, second: Int = 0) {
print("$day-$month-$year $hour:$minute:$second")
}
因为象这样的kotlin默认打印功能
/** Prints the given message and newline to the standard output stream. */
public expect fun println(message: Any?)
/** Prints the given message to the standard output stream. */
public expect fun print(message: Any?)
所以您不能在打印功能中发送双重参数。请使用
print("$day-$month-$year $hour:$minute:$second")
代替
print("TEST", "$day-$month-$year $hour:$minute:$second")
答案 1 :(得分:0)
在线错误
createDate(1,7,1997)
因为您调用了createDate()函数,但是参数是不匹配。
让我们覆盖此功能:
createDate(1,7,1997,5,24,15)