android - 如何使用 Kotlin 在内部创建接口方法?
问题描述
我写了一个简单的 SwipeLayout 库。我的目标是通过 GitHub 将它发布给包括我自己在内的所有人使用。当我决定封装一些方法时,我遇到了一个问题。我希望有一个内部修改器来启动并解决问题,但我找不到合适的方法来做到这一点。下面是一些代码和解释:
class SwipeLayout (...) : FrameLayout(...),
BackgroundViewsVisibilityController, ... {
private val backgroundController = BackgroundController(
this //(BackgroundViewsVisibilityController)
)
override fun revealLeftUnderView() {...}
override fun hideLeftUnderView() {...}
override fun revealRightUnderView() {...}
override fun hideRightUnderView() {...}
}
interface BackgroundViewsVisibilityController {
fun revealLeftUnderView()
fun revealRightUnderView()
fun hideLeftUnderView()
fun hideRightUnderView()
}
这些是对用户隐藏的方法。我怎样才能最好地实现它?
解决方案
internal
告诉编译器这个接口、类或函数只能从当前模块调用。当您发布一个库时,internal
不能从您的库中调用该接口(不使用反射)。
您想SwipeLayout
公开(供第三方使用),因此您不能同时制作接口BackgroundViewsVisibilityController
internal
,因为您的类正在扩展该接口。
如果您真的希望客户端不能从您的接口调用函数,请考虑没有接口(因为接口通常是供调用者使用的)但private
在以下位置创建这些函数SwipeLayout
:
class SwipeLayout (...) : FrameLayout(...)
private fun revealLeftUnderView() {...}
private fun hideLeftUnderView() {...}
private fun revealRightUnderView() {...}
private fun hideRightUnderView() {...}
}
推荐阅读
- google-cloud-firestore - 如何为这样的任务正确构建模型?
- javascript - livewire wire:submit.prevent 仅在通过提交按钮提交表单时有效
- javascript - 为什么我的代码 JavaScript 代码会自我召回
- flutter - 在 Windows Server 2012RE 上颤振 Windows http 剂量工作
- swift - Xcode Error Embedded binary's bundle identifier is not prefixed with parent app's bundle identifier
- python - 如何为 Scrapy Web Scraper 创建测试用例和数据点验证器
- caching - 计算机架构与缓存块大小的关系
- c# - 使用用户名、密码、公司 ID 调用 Rest API
- python - Pi 设置 - 如何简化
- mysql - 每月刷新 MATERIALIZED VIEW