小编Ren*_*nis的帖子

具有默认函数参数值的 Kotlin 接口

我想在实现接口的类中使用函数参数的默认值,如下所示:

interface FileStoreService {
    fun storeFile(path: String, payload: InputStream, type: MediaType, replace: Boolean = true)
}
Run Code Online (Sandbox Code Playgroud)
class LocalFileStoreService : FileStoreService {

    override fun storeFile(path: String, payload: InputStream, type: MediaType, replace: Boolean /* ?? */) {
        // ....
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,这是编译的内容,这是未编译的内容:

KO:不允许重写函数为其参数指定默认值

class LocalFileStoreService : FileStoreService {

    override fun storeFile(path: String, payload: InputStream, type: MediaType, replace: Boolean = true) {
        // ....
    }
}
Run Code Online (Sandbox Code Playgroud)

KO:“LocalFileStoreService”类不是抽象的,也没有实现抽象成员 public Abstract fun storeFile(path: String, Payload: InputStream, type: MediaType): 在 fqn...FileStoreService 中定义的单元

class LocalFileStoreService …
Run Code Online (Sandbox Code Playgroud)

kotlin

7
推荐指数
1
解决办法
1万
查看次数

标签 统计

kotlin ×1