kotlin - 私有委托方法
问题描述
我在 Kotlin 中使用类委托,想知道是否可以在 Kotlin 中将委托方法设为私有
interface A{
fun test(name: String)
}
class A1:A{
fun test(name: String): String = name
}
interface C{
fun myTest(name: String)
}
class C1(a:A){
fun myTest(name: String) = a.test(name)
}
class B(a:A): C by C1(a) {
// I can call "mytest" here
fun anotherMethod() = myTest("hi")
//But I want to make "myTest" private
}
val b = B(A1())
//This should not be possible
//b.myTest()
解决方案
接口用于公开API的功能,如果B是A,那么它必须有一个公共成员测试。
如果您不希望 test() 作为公共成员使用,则不应实施 A:
class B(val a: A) {
fun anotherMethod() = a.test("hi")
}
推荐阅读
- python - OSError:处理大文件时Jupyter docker容器中的[Errno 107]
- docker - 我可以将 Google Container Optimized OS 用作安全的容器沙箱吗?
- python - 如何在pygame中防止垃圾邮件子弹
- rest - Rest API 设计 - 图像标记应用程序
- java - http 出站网关的 URL 中的路径参数
- r - 如何正确重写 R Shiny 的函数 icon() 以包含 Font-Awesome Pro 图标?
- tcl - 使用 tcl/tk 的临时目录
- java - 如何定义自定义组合谓词?
- reactjs - Material-ui Snackbar 弹出屏幕
- database - SOLR 中的数据导入